SwiftUIMenu is a component that allows you to implement Menu Navigation Pattern in your App. Menu
presents a content and a list of of options that's easily displayed by swiping or reacting to an external event such as a navbar button tap.
- iOS 13.0+
- macOS 10.15+
- watchOS 6.0+
- Swift 5.1+
pod 'SwiftUIMenu'
Go to XCode:
- File → Swift Packages → Add Package Dependency...
- Use the URL https://github.com/fermoya/SwiftUIMenu.git
github "fermoya/SwiftUIMenu"
You can create a Menu
by passing:
- Binding to the selected index
- Binding to determine if
Menu
is open - Array of items to populate the menu
KeyPath
to an identifier.ViewBuilder
to build each rowViewBuilder
to build the content for the selected index
struct MenuItem: Equatable {
var name: String
var color: Color
}
@State var index = 0
@State var isMenuOpen = false
let menuItems = [
MenuItem(name: "Section 1", color: .blue),
...
]
var body: some View {
Menu(indexSelected: self.$index,
isOpen: self.$isMenuOpen,
menuItems: menuItems,
id: \.name,
menuItemRow: { index in
Text("Option \(index)")
},
menuItemContent: { section in
Text("Welcome to section \(section)")
})
})
}
All examples require
import SwiftUIMenu
at the top of the source file.
There're plenty of view-modifiers available to customize your Menu
and give it a personal touch. You can specify its position, style or add a header, among others.
By default, Menu
is configured to:
- Reveal from the left
- Have an overlap style
- Take up the whole screen
- Allow dragging
Changes the position of the menu so that it reveals from the left or right of the screen.
Menu(...)
.alignment(.right)
Use these modifiers to add a header and/or footer to Menu
.
Menu(...)
.header { MyMenuHeader() }
.footer { MyMenuFooter() }
<img src="resources/header-footer.gif" alt=Menu with header and footer" height="640"/>
Adds a different effect to the way Menu
is revealed.
Menu(...)
.style(.stretch)
You can also:
- Change the ratio of available space that
Menu
will take up when open withrevealRatio
- Disable the
DragGesture
withdisableDragging
- Make the content dismiss
Menu
by a simple tap withallowContentTap()
- Shade the content when
Menu
is open withshadeContent
Menu(...)
.revealRatio(0.8)
.shadeContent()
For more information, please check the sample app.
Your feedback is more than welcome, don't hesitate to open an issue or ping me at fmdr.ct@gmail.com.
Feel free to collaborate and make this framework better.
SwiftUIMenu
is available under the MIT license. See the LICENSE file for more info.