[英]iOS: How to place a button in Navigation bar parallel to NavigationBar Title?
您是否嘗試過放置 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)
如果您想使用SwiftUI
在NavigationBar
中添加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.