使用toolbar添加button
10个月前 • 436次点击 • 来自 移动端
标签: SwiftUI
SwiftUI 最新的modifier - toolbar()
可以更方便在NavigationView各个位置上添加控件,例如:
底部添加Button
NavigationView {
Text("Hello, World!").padding()
.navigationTitle("SwiftUI")
.toolbar {
ToolbarItem(placement: .bottomBar) {
Button("Press Me") {
print("Pressed")
}
}
}
}
底部添加2个Button
NavigationView {
Text("Hello, World!").padding()
.navigationTitle("SwiftUI")
.toolbar {
ToolbarItemGroup(placement: .bottomBar) {
Button("First") {
print("Pressed")
}
Button("Second") {
print("Pressed")
}
}
}
}
其中,placement
接收ToolbarItemPlacement
,使得控件能出现在不同位置:
/// A structure that defines the placement of a toolbar item.
///
/// There are two types of placements:
/// - Semantic placements, such as ``ToolbarItemPlacement/principal`` and
/// ``ToolbarItemPlacement/navigation``, denote the intent of the
/// item being added. SwiftUI determines the appropriate placement for
/// the item based on this intent and its surrounding context, like the
/// current platform.
/// - Positional placements, such as
/// ``ToolbarItemPlacement/navigationBarLeading``, denote a precise
/// placement for the item, usually for a particular platform.
///
/// In compact horizontal size classes, the system limits both the leading
/// and the trailing positions of the navigation bar to a single item each.
@available(iOS 14.0, macOS 11.0, tvOS 14.0, watchOS 7.0, *)
public struct ToolbarItemPlacement {
....
}