使用toolbar添加button

2周前 56次点击 来自 iOS

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 {
	....
}
Card image cap
开发者雷

尘世间一个小小的开发者,每天增加一些无聊的知识,就不会无聊了

要加油~~~

技术文档 >> 系列应用 >>
热推应用
Let'sLearnSwift
学习Swift的入门教程
PyPie
Python is as good as Pie
标签