xhr Asked:2020-10-26 20:41:56 +0000 UTC2020-10-26 20:41:56 +0000 UTC 2020-10-26 20:41:56 +0000 UTC 用您自己的图像替换后退按钮的最佳方式 772 用自定义图像替换 UINavigationBar for iOS10+ 中的后退按钮的最佳方法是什么?(不需要标题) ios10 1 个回答 Voted Best Answer xhr 2020-03-11T22:55:12Z2020-03-11T22:55:12Z 这样做的: 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 } } 额外:关于如何更改标准后退按钮的图像以及如何定位它的一些建议: https://sarunw.com/posts/how-to-change-back-button-image/
这样做的:
额外:关于如何更改标准后退按钮的图像以及如何定位它的一些建议:
https://sarunw.com/posts/how-to-change-back-button-image/