簡體   English   中英

iOS:如何在導航欄中與 NavigationBar Title 平行放置一個按鈕?

[英]iOS: How to place a button in Navigation bar parallel to NavigationBar Title?

我想在導航欄標題的同一級別上放置一個按鈕。

就像下圖一樣。

也需要 swiftui 中的解決方案。

像這樣

您是否嘗試過放置 UIBarButton 項? 您可以將其放置在 storyboard 的導航欄上。 您可以像下面這樣以編程方式創建它並將其設置為導航欄的導航項

let btn1 = UIButton(type: .custom)
btn1.setImage(UIImage(named: "imagename"), for: .normal)
btn1.addTarget(self, action: #selector(methodname), for: .touchUpInside)
let item1 = UIBarButtonItem(customView: btn1)
self.navigationItem.setRightBarButtonItems([item1], animated: true)

如果您想使用SwiftUINavigationBar中添加Button ,請嘗試以下代碼。

struct ContentView: View {
  var body: some View {
    NavigationView {
        VStack{
            Text("Hello World!!")
        }
        .navigationBarItems(trailing:
            HStack {
                Button(action: {
                   //action
                }) {
                    Image(systemName: "trash")
                        .frame(width: 25, height: 25, alignment: .center)
                        .foregroundColor(.black)
                }
                Spacer().frame(width: 10)
                Button(action: {
                    //action
                }) {
                    Image(systemName: "link")
                       .frame(width: 25, height: 25, alignment: .center)
                       .foregroundColor(.black)
                }
            }
        )
    }
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM