简体   繁体   中英

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

I want to place a button on the same level of Navigation Bar Title.

Like the image below.

Need the solution in swiftui as well.

像这样

Have you tried placing the UIBarButton item? You can place it on navigation bar in the storyboard. You can create it programmatically like below and set it to navigation item of the navigation bar

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)

Try below code if you want to add Button in NavigationBar using SwiftUI .

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)
                }
            }
        )
    }
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM