我使用约束制作了自定义UINavigationBar。我在添加后退按钮时遇到问题。我可以制作一个UI按钮并将其分层,但我不想这样做,除非我必须这样做。
以下是使用的相关代码。
let navBar: UINavigationBar = {
let myNav = UINavigationBar()
myNav.translatesAutoresizingMaskIntoConstraints = false
return myNav
}()
func setUpNavBarView(){
self.navBar.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
self.navBar.topAnchor.constraint(equalTo: view.topAnchor as NSLayoutAnchor<NSLayoutYAxisAnchor>).isActive = true
self.navBar.widthAnchor.constraint(equalTo: view.widthAnchor).isActive = true
self.navBar.heightAnchor.constraint(equalToConstant: 60).isActive = true
self.navBar.barTintColor = UIColor.red
self.navBar.isTranslucent = false
}
答案 0 :(得分:0)
lazy var backButton: UIBarButtonItem = {
let button = UIBarButtonItem(image: "Image", style: .plain, target: self, action: #selector(backButtonTapped))
return button
}()
navigationItem.setLeftBarButton(backButton, animated: false)
func backButtonTapped() {
//Do transition here
}