美文网首页
iOS11处理自定义leftBarButtonItems和rig

iOS11处理自定义leftBarButtonItems和rig

作者: galenu | 来源:发表于2019-08-08 15:57 被阅读0次

iOS11之后导航栏默认层级发生改变,自定义的左右view默认放在了_UIButtonBarStackView里面,而左边的UIButtonBarStackView默认距离导航栏左边20间距,右边的UIButtonBarStackView默认距离导航栏右边20间距,所以可以遍历找到这个约束修改为0,便能取消间隔。

image.png

还有一种方案是把自定义的按钮放在一个父View里,让View整体想左(或向右)便宜,iOS默认可以显示超出父视图的部分,但是默认超出父视图的部分不能响应事件,所以点击超出部分没有反应,不推荐这种做法

iOS11之前是直接把自定义的视图放在了UINavigationBar上, 层级如下图:
可以通过创建占位空间(UIBarButtonItem(barButtonSystemItem: .fixedSpace, target: nil, action: nil))来调整位置

image.png
import UIKit

class NavigationController: UINavigationController, UIGestureRecognizerDelegate {
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // 设置半透明
        self.navigationBar.isTranslucent = true
        
        // navigationBar背景颜色
        self.navigationBar.barTintColor = UIColor.white
        
        // 左右按钮文本
        self.navigationBar.tintColor = UIColor.rgb(51, 51, 51)
        
        // 标题文本
        self.navigationBar.titleTextAttributes = [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 18),
                                                  NSAttributedString.Key.foregroundColor: UIColor.rgb(51, 51, 51)]
        
        // 返回手势可用
        self.interactivePopGestureRecognizer?.isEnabled = true
        self.interactivePopGestureRecognizer?.delegate = self
        
        // 背景
//        self.navigationBar.setBackgroundImage(UIImage(), for: .default)
        
        // 隐藏navigationBar分割线
        self.navigationBar.shadowImage = UIImage()
        
//        self.navigationBar.isHidden = true

    }
    
    override func pushViewController(_ viewController: UIViewController, animated: Bool) {
        
        if self.viewControllers.count > 0 {
            viewController.hidesBottomBarWhenPushed = true
            // 替换系统返回键
            viewController.addNavigationBarLeftView(self.customLeftBackButton())
        }
        super.pushViewController(viewController, animated: animated)
    }
    
    // 跟控制器不响应返回手势
    func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
        return self.viewControllers.count > 1
    }
    
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        
        // ios11调整navigationBar上按钮的左右间距,默认左右各20
        self.adjustNavigationBarItemsSpacing()
    }
    
    /// 自定义返回按钮
    private func customLeftBackButton() -> UIButton {
        let backBtn = UIButton(frame: CGRect(x: 0, y: 0, width: 100, height: 44))
        let image = UIImage(named: "navigaitonbar_back_btn_normal")
        backBtn.setImage(image, for: .normal)
        backBtn.imageView?.contentMode = .scaleAspectFit
        backBtn.imageEdgeInsets = UIEdgeInsets(top: 0, left: -60, bottom: 0, right: 0)
        backBtn.addTarget(self, action: #selector(backBtnClicked), for: .touchUpInside)
        return backBtn
        
    }
    
    @objc private func backBtnClicked() {
        self.popViewController(animated: true)
    }
    
    /// ios11调整navigationBar上按钮的左右间距,默认左右各20
    private func adjustNavigationBarItemsSpacing() {
        if #available(iOS 11, *) {
            for subview in self.navigationBar.subviews {
                if subview.className.contains("UINavigationBarContentView"){
                    for constant in subview.constraints {
                        if constant.constant == 20 || constant.constant == -20 {
                            constant.constant = 0                    }
                    }
                }
            }
        }
    }
    
}

extension UIViewController {
    
    /// 给系统导航栏添加左边View(删除左边20间隔)
    ///
    /// - Parameter leftView: leftView
    func addNavigationBarLeftView(_ leftView: UIView) {
        let leftItem = UIBarButtonItem(customView: leftView)
        if #available(iOS 11, *) {
            self.navigationItem.leftBarButtonItems = [leftItem]
        } else {
            // 用于消除左边空隙,要不然按钮顶不到最左边
            let leftSpace = UIBarButtonItem(barButtonSystemItem: .fixedSpace, target: nil, action: nil)
            leftSpace.width = -20
            self.navigationItem.leftBarButtonItems = [leftSpace, leftItem]
        }
    }
    
    /// 给系统导航栏添加右边View(删除右边20间隔)
    ///
    /// - Parameter rightView: rightView
    func addNavigationBarRightView(_ rightView: UIView) {
        let rightItem = UIBarButtonItem(customView: rightView)
        if #available(iOS 11, *) {
            self.navigationItem.rightBarButtonItems = [rightItem]
        } else {
            // 用于消除右边空隙,要不然按钮顶不到最右边
            let rightSpace = UIBarButtonItem(barButtonSystemItem: .fixedSpace, target: nil, action: nil)
            rightSpace.width = -20
            self.navigationItem.rightBarButtonItems = [rightSpace, rightItem]
        }
    }
}

相关文章

网友评论

      本文标题:iOS11处理自定义leftBarButtonItems和rig

      本文链接:https://www.haomeiwen.com/subject/xfixjctx.html