[英]How to add a item on title bar in SwiftUI?
根据您的需要进行调整:
struct ContentView: View {
var body: some View {
NavigationView {
VStack {
Text("Test")
}
.navigationBarTitle("Summary")
.navigationBarItems(trailing: Button(action: { }, label: {
Image("Your image name")
})
)
}
}
}
您好,我已附上您需要的代码! 请根据您的需要调整图像。
\\call this function in viewdidLoad()
func addNavBarImage() {
let navController = navigationController!
let image = UIImage(named: "Image.png") //Your logo url here
let imageView = UIImageView(image: image)
let bannerWidth = navController.navigationBar.frame.size.width
let bannerHeight = navController.navigationBar.frame.size.height
let bannerX = bannerWidth / 2.5 - (image?.size.width)! / 2.5
let bannerY = bannerHeight / 2 - (image?.size.height)! / 2
imageView.frame = CGRect(x: bannerX, y: bannerY, width: bannerWidth, height: bannerHeight)
imageView.contentMode = .scaleAspectFit
navigationItem.titleView = imageView
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.