简体   繁体   English

如何在导航栏 swift ui 中设置左右按钮(前导/尾随)?

[英]How to set buttons in left and right(leading/trailing) in navigation bar swift ui?

I want to set one button in left(leading) of navigation bar and one button in right(trailing) of navigation bar in swift ui.我想在 swift ui 的导航栏左侧(前导)设置一个按钮,在导航栏右侧(尾随)设置一个按钮。
Please help me to do it.请帮我做。

.navigationBarItems(leading:
  HStack {
   Button("About") {
    print("About tapped!")
   }
  }
 )

Use the following modifier使用以下修饰符

.navigationBarItems(leading:
   Button("About") {
    print("About tapped!")
   },
 trailing: 
   Button("Settings") {
    print("Settings tapped!")
   }
)

Try this code for set multiple buttons:试试这个代码来设置多个按钮:

.navigationBarItems(
   leading:
     HStack {
       Button("About") {
         print("About tapped!")
       }
       Button("Call") {
         print("Call tapped!")
       }
     },

   trailing:
     HStack {
       Button("Settings") {
         print("Settings tapped!")
       }
       Button("Contacts") {
         print("Contacts tapped!")
       }           
     }
  )

navigationBarItems is deprecated.不推荐使用 navigationBarItems。 Hence below code will work.因此下面的代码将起作用。

.toolbar {
                    
                    ToolbarItem(placement:.navigationBarTrailing) {
                        HStack{
                            Button("Add"){
                                saveDetails()
                                dismiss()
                            }
                        }
                       
                    }
                    ToolbarItem(placement: .navigationBarLeading) {
                        Button("Cancel"){
                            dismiss()
                        }
                    }
                }

In this Add button will be shown as right button and cancel as left.在此添加按钮将显示为右键,取消显示为左键。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SwiftUI 中的左右填充(不是前导和尾随) - Left and right padding (not leading and trailing) in SwiftUI 如何在Swift 4中从左到右更改导航动画? - how to change navigation animation to left to right in swift 4? swift:使导航栏从右到左 (rtl) 使用左右项目,例如 back - swift: make navigation bar Right-to-left (rtl) with both right and left items like back flutter 小部件中是否存在前导/尾随约束而不是左/右? - Is there leading/trailing constraints in flutter widget instead of left/right? SwiftUI:滑块位于导航栏项目的前导/尾随时的奇怪行为 - SwiftUI: Weird behaviour of Slider when it is in the leading/trailing of navigation bar items 如何设置右导航栏按钮标题 - How to set right navigation bar button title 如何限制导航按钮一直向左或向右? (iOS) - How to constrain navigation buttons all the way to the left or right? (iOS) 如何在 Swift 中为 UITableView 设置左右边距 - How to set left and right margin for UITableView in Swift 在 swift 中将内容的前导边距设置为导航栏大标题的前导 - setting leading margin of content to the leading of navigation bar large title in swift iOS中Navigation bar的自定义视图如何缩小左右间距 - How to reduce the left and right gaps in custom view for Navigation bar in iOS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM