如何向自定义UINavigationBar添加后退按钮

时间:2017-02-04 01:05:24

标签: ios swift uinavigationbar back-button

我使用约束制作了自定义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


}

1 个答案:

答案 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
}