What is the best way to replace the Back button in UINavigationBar for iOS10 + with a custom picture? (no title required)
1 answer
Did this:
class CustomViewController: UIViewController, UIGestureRecognizerDelegate { // MARK: - View Lifecycle override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. addBackButton() } override func viewDidAppear(_ animated: Bool) { super.viewDidAppear(animated) navigationController?.interactivePopGestureRecognizer?.isEnabled = true } // MARK: - UI Events @objc func backButtonTapped() { _ = self.navigationController?.popViewController(animated: true) } // MARK: - Private Methods private func addBackButton() { let button = UIButton(type: .custom) button.setImage(UIImage(named: "iconNavBack"), for: .normal) button.addTarget(self, action: #selector(backButtonTapped), for: .touchUpInside) var buttonFrame = button.frame var buttonHeight: CGFloat = 44 if let navBarHeight = navigationController?.navigationBar.frame.size.height { buttonHeight = navBarHeight } buttonFrame.size = CGSize(width: 44, height: buttonHeight) // button.layer.borderWidth = 1 // button.layer.borderColor = UIColor.orange.cgColor button.frame = buttonFrame button.contentEdgeInsets = UIEdgeInsets(top: 0, left: -24, bottom: 0, right: 0) // NOTE: ΡΠ΄Π²ΠΈΠ³Π°Π΅ΠΌ Π»Π΅Π²Π΅Π΅ Π½Π΅ ΡΠ°ΠΌΡ ΠΊΠ½ΠΎΠΏΠΊΡ, Π½ΠΎ ΠΈΠ·ΠΎΠ±ΡΠ°ΠΆΠ΅Π½ΠΈΠ΅ Π½Π° Π½Π΅ΠΉ (Π΅ΡΠ»ΠΈ Π½ΡΠΆΠ½ΠΎ ΠΏΠΎΠ΄Π²ΠΈΠ½ΡΡΡ ΠΏΠΎ Π΄ΠΈΠ·Π°ΠΉΠ½Ρ) navigationItem.leftBarButtonItem = UIBarButtonItem(customView: button) self.navigationController?.interactivePopGestureRecognizer?.delegate = self } } |