part1、分析新导航的结构
首先看一下下面代码:
let leftButton: UIButton = UIButton(type: .custom)
leftButton.setTitle("左一", for: .normal)
leftButton.frame = CGRect(x: 0, y: 0, width: 60, height: 30)
leftButton.backgroundColor = .green
let leftButton_1: UIButton = UIButton(type: .custom)
leftButton_1.setTitle("左二", for: .normal)
leftButton_1.frame = CGRect(x: 0, y: 0, width: 60, height: 30)
leftButton_1.backgroundColor = .green
let leftBarItem:UIBarButtonItem = UIBarButtonItem(customView: leftButton)
let leftBarItem_1:UIBarButtonItem = UIBarButtonItem(customView: leftButton_1)
self.navigationItem.leftBarButtonItems = [leftBarItem, leftBarItem_1]
let rightButton: UIButton = UIButton(type: .custom)
rightButton.setTitle("右一", for: .normal)
rightButton.frame = CGRect(x: 0, y: 0, width: 30, height: 30)
rightButton.backgroundColor = .green
let rightButton_1: UIButton = UIButton(type: .custom)
rightButton_1.setTitle("右二", for: .normal)
rightButton_1.frame = CGRect(x: 0, y: 0, width: 50, height: 30)
rightButton_1.backgroundColor = .green
let rightBarItem:UIBarButtonItem = UIBarButtonItem(customView: rightButton)
let rightBarItem_1:UIBarButtonItem = UIBarButtonItem(customView: rightButton_1)
self.navigationItem.rightBarButtonItems = [rightBarItem, rightBarItem_1]
let titleLabel:UILabel = UILabel(frame: CGRect(x: 0, y: 0, width: 300, height: 30))
titleLabel.textAlignment = .center
titleLabel.text = "titleView栏"
self.navigationItem.titleView = titleLabel
左边两个宽度均为60的item,右边一个30一个50的item,中间一个很宽(300)的titleVIew,为什么这么设置呢?后面再说。。。
效果如下:
看起来怪怪的有没有、、、(左右item距离屏幕边距,各边items之间的边距,titleView的位置偏移)
这是为什么呢?我们看下导航的层级结构:
哦呦~还真是大变样子咯!
_UIBarBackground 显然就是背景样式咯,包含一个UIImageView一个UIVisualEffectView,那就可以理解磨砂透明效果怎么来的啦。不过,我们今天先不关注这个东西。
我们看看_UINavigationBarContentView:
包含两个_UIButtonBarStackView,一个_UITAMICAdaptorView
_UIButtonBarStackView很显然就是分别装左右两边的BarItems啦,
_UITAMICAdaptorView显然就是装titleView啦。
可是为啥子会是那种显示效果呢?我们随便打印下他们的frame看看:
Printing description of $15:
<_UIButtonBarStackView: 0x7fa1b1529cc0; frame = (20 0; 128 44); layer = <CALayer: 0x600000224160>>
Printing description of $16:
<_UIButtonBarStackView: 0x7fa1b152c1e0; frame = (299 0; 95 44); layer = <CALayer: 0x600000225f40>>
Printing description of $17:
<_UITAMICAdaptorView: 0x7fa1b152d770; frame = (148 7; 151 30); autoresizesSubviews = NO; layer = <CALayer: 0x600000223480>>
那就是左边的_UIButtonBarStackView是从20起,右边_UIButtonBarStackView距离边界20,中间的_UITAMICAdaptorView填充左右两个_UIButtonBarStackView之间的部分,没有间距。
具体分析
先看左边_UIButtonBarStackView
很显然是两个item加上一个_UITAMICAdaptorView了,上面代码设置我们知道理论上两个item的width都是60了,打印下看看:
Printing description of $18: <_UITAMICAdaptorView: 0x7fa1b152e8f0; frame = (0 5; 60 34); autoresizesSubviews = NO; layer = <CALayer: 0x6000002254c0>> Printing description of $19: <UIView: 0x7fa1b15233f0; frame = (60 22; 8 0); layer = <CALayer: 0x600000225f00>> Printing description of $20: <_UITAMICAdaptorView: 0x7fa1b15235d0; frame = (68 5; 60 34); autoresizesSubviews = NO; layer = <CALayer: 0x6000002251a0>>
恩、、、两个60width的item没毛病,8的space也没毛病,60+60+8=128 。老铁没毛病!美滋滋~
再看右边_UIButtonBarStackView
层级结构和左边一样的,上面代码设置我们知道理论上一个item是30width,一个是50width,再打印一下:
Printing description of $21: <_UITAMICAdaptorView: 0x7fa1b1431440; frame = (0 5; 50 34); autoresizesSubviews = NO; layer = <CALayer: 0x608000225aa0>> Printing description of $22: <UIView: 0x7fa1b1432660; frame = (50 22; 8 0); layer = <CALayer: 0x608000227620>> Printing description of $23: <_UITAMICAdaptorView: 0x7fa1b1432840; frame = (58 5; 37 34); autoresizesSubviews = NO; layer = <CALayer: 0x608000227280>>
看到没,老铁,问题来了! 这有个家伙宽度变34了。不用怀疑代码问题,这是设置的30宽度不足以显示“右一”两个字,StackView给layout了。(这里大家需要注意一下)
两边分析完了,我们来看看中间的titleView:
也许大家在刚开始的打印中就发现问题了,titleView的width才151,我们明明设置的是300啊。好吧,看来是在contentView中先把两边的StackView安置好了之后才将剩下空间分给titleView的。所以,如果两边的items不能做到对称的话,那么titleView就不居中了。。。
我们可以通过打印下_UINavigationBarContentView的layoutConstraints来简单看下各控件的关系。
po view.superview.constraints
<__NSArrayI 0x7f8e51728790>(
<NSLayoutConstraint:0x60c00008a190 H:|-(0)-[UILayoutGuide:0x60c0001b67a0'BackButtonGuide(0x7f8e515088e0)'] (active, names: '|':_UINavigationBarContentView:0x7f8e51508210 )>,
<NSLayoutConstraint:0x60c00008a2d0 UILayoutGuide:0x60c0001b67a0'BackButtonGuide(0x7f8e515088e0)'.trailing >= _UINavigationBarContentView:0x7f8e51508210.leading (active)>,
<NSLayoutConstraint:0x60c00008a230 H:[UILayoutGuide:0x60c0001b67a0'BackButtonGuide(0x7f8e515088e0)']-(0)-[UILayoutGuide:0x60c0001b6880'LeadingBarGuide(0x7f8e515088e0)'] (active)>,
<NSLayoutConstraint:0x60c00008a140 H:[UILayoutGuide:0x60c0001b6880'LeadingBarGuide(0x7f8e515088e0)']-(0)-[UILayoutGuide:0x60c0001b6960'TitleView(0x7f8e515088e0)'] (active)>,
<NSLayoutConstraint:0x60c00008a0f0 H:[UILayoutGuide:0x60c0001b6960'TitleView(0x7f8e515088e0)']-(0)-[UILayoutGuide:0x60c0001b6a40'TrailingBarGuide(0x7f8e515088e0)'] (active)>,
<NSLayoutConstraint:0x60c00008a0a0 UILayoutGuide:0x60c0001b6a40'TrailingBarGuide(0x7f8e515088e0)'.trailing == _UINavigationBarContentView:0x7f8e51508210.trailing priority:999 (active)>,
<NSLayoutConstraint:0x60c00008bf90 UILayoutGuide:0x60c0001b67a0'BackButtonGuide(0x7f8e515088e0)'.width == 0 priority:50 (active)>,
<NSLayoutConstraint:0x60c00008c030 UILayoutGuide:0x60c0001b6880'LeadingBarGuide(0x7f8e515088e0)'.width == 0 priority:50 (active)>,
<NSLayoutConstraint:0x60c00008bfe0 UILayoutGuide:0x60c0001b6960'TitleView(0x7f8e515088e0)'.width == UILayoutGuide:0x60c0001b6b20'UIViewLayoutMarginsGuide'.width priority:50 (active)>,
<NSLayoutConstraint:0x60c00008c2b0 UILayoutGuide:0x60c0001b6a40'TrailingBarGuide(0x7f8e515088e0)'.width == 0 priority:50 (active)>,
<NSLayoutConstraint:0x600000089740 V:|-(0)-[UILayoutGuide:0x60c0001b67a0'BackButtonGuide(0x7f8e515088e0)'] (active, names: '|':_UINavigationBarContentView:0x7f8e51508210 )>,
<NSLayoutConstraint:0x600000089650 V:|-(0)-[UILayoutGuide:0x60c0001b6880'LeadingBarGuide(0x7f8e515088e0)'] (active, names: '|':_UINavigationBarContentView:0x7f8e51508210 )>,
<NSLayoutConstraint:0x60000008a6e0 V:|-(0)-[UILayoutGuide:0x60c0001b6960'TitleView(0x7f8e515088e0)'] (active, names: '|':_UINavigationBarContentView:0x7f8e51508210 )>,
<NSLayoutConstraint:0x60000008a910 V:|-(0)-[UILayoutGuide:0x60c0001b6a40'TrailingBarGuide(0x7f8e515088e0)'] (active, names: '|':_UINavigationBarContentView:0x7f8e51508210 )>,
<NSLayoutConstraint:0x60000008afa0 UILayoutGuide:0x60c0001b67a0'BackButtonGuide(0x7f8e515088e0)'.height == 44 (active)>,
<NSLayoutConstraint:0x60000008b1d0 UILayoutGuide:0x60c0001b6880'LeadingBarGuide(0x7f8e515088e0)'.height == 44 (active)>,
<NSLayoutConstraint:0x60000008b3b0 UILayoutGuide:0x60c0001b6960'TitleView(0x7f8e515088e0)'.height == 44 (active)>,
<NSLayoutConstraint:0x60000008b220 UILayoutGuide:0x60c0001b6a40'TrailingBarGuide(0x7f8e515088e0)'.height == 44 (active)>,
<NSLayoutConstraint:0x60c00008c6c0 _UIButtonBarStackView:0x7f8e51502e80.leading == UILayoutGuide:0x60c0001b6880'LeadingBarGuide(0x7f8e515088e0)'.leading (active)>,
<NSLayoutConstraint:0x60c00008c710 _UIButtonBarStackView:0x7f8e51502e80.trailing == UILayoutGuide:0x60c0001b6880'LeadingBarGuide(0x7f8e515088e0)'.trailing (active)>,
<NSLayoutConstraint:0x60c00008c760 _UIButtonBarStackView:0x7f8e51502e80.top == UILayoutGuide:0x60c0001b6880'LeadingBarGuide(0x7f8e515088e0)'.top (active)>,
<NSLayoutConstraint:0x60c00008c7b0 _UIButtonBarStackView:0x7f8e51502e80.bottom == UILayoutGuide:0x60c0001b6880'LeadingBarGuide(0x7f8e515088e0)'.bottom (active)>,
<NSLayoutConstraint:0x60c00008ce90 _UIButtonBarStackView:0x7f8e5150bbe0.leading == UILayoutGuide:0x60c0001b6a40'TrailingBarGuide(0x7f8e515088e0)'.leading (active)>,
<NSLayoutConstraint:0x60c00008cee0 _UIButtonBarStackView:0x7f8e5150bbe0.trailing == UILayoutGuide:0x60c0001b6a40'TrailingBarGuide(0x7f8e515088e0)'.trailing (active)>,
<NSLayoutConstraint:0x60c00008cf30 _UIButtonBarStackView:0x7f8e5150bbe0.top == UILayoutGuide:0x60c0001b6a40'TrailingBarGuide(0x7f8e515088e0)'.top (active)>,
<NSLayoutConstraint:0x60c00008cf80 _UIButtonBarStackView:0x7f8e5150bbe0.bottom == UILayoutGuide:0x60c0001b6a40'TrailingBarGuide(0x7f8e515088e0)'.bottom (active)>,
<NSLayoutConstraint:0x60c00008c210 'UIView-bottomMargin-guide-constraint' V:[UILayoutGuide:0x60c0001b6b20'UIViewLayoutMarginsGuide']-(0)-| (active, names: '|':_UINavigationBarContentView:0x7f8e51508210 )>,
<NSLayoutConstraint:0x6080000922a0 'UIView-Encapsulated-Layout-Height' _UINavigationBarContentView:0x7f8e51508210.height == 44 (active)>,
<NSAutoresizingMaskLayoutConstraint:0x608000092340 h=--& v=--& 'UIView-Encapsulated-Layout-Left' _UINavigationBarContentView:0x7f8e51508210.minX == 0 (active, names: '|':UINavigationBar:0x7f8e51505bc0 )>,
<NSAutoresizingMaskLayoutConstraint:0x608000092480 h=--& v=--& 'UIView-Encapsulated-Layout-Top' _UINavigationBarContentView:0x7f8e51508210.minY == 0 (active, names: '|':UINavigationBar:0x7f8e51505bc0 )>,
<NSLayoutConstraint:0x608000092250 'UIView-Encapsulated-Layout-Width' _UINavigationBarContentView:0x7f8e51508210.width == 414 (active)>,
<NSLayoutConstraint:0x60c00008c1c0 'UIView-leftMargin-guide-constraint' H:|-(0)-[UILayoutGuide:0x60c0001b6b20'UIViewLayoutMarginsGuide'](LTR) (active, names: '|':_UINavigationBarContentView:0x7f8e51508210 )>,
<NSLayoutConstraint:0x60c00008c260 'UIView-rightMargin-guide-constraint' H:[UILayoutGuide:0x60c0001b6b20'UIViewLayoutMarginsGuide']-(0)-|(LTR) (active, names: '|':_UINavigationBarContentView:0x7f8e51508210 )>,
<NSLayoutConstraint:0x60c00008c080 'UIView-topMargin-guide-constraint' V:|-(0)-[UILayoutGuide:0x60c0001b6b20'UIViewLayoutMarginsGuide'] (active, names: '|':_UINavigationBarContentView:0x7f8e51508210 )>
)
简单分析可大概得到下面的关系:
LeadingBarGuide
-(0)-TitleView
-(0)-TrailingBarGuide
part2、解决方案
我们这里主要解决左右_UIButtonBarStackView距离屏幕边距,和其中各个items间距的问题。至于titleView的问题,相信看过之后大家也有相应的思路了。
主要有两种方式:
1.修改LeadingBarGuide
,TrailingBarGuide
的layout,遍历_UIButtonBarStackView.subViews
得到中间的填充View修改width。
2.直接修改左右_UIButtonBarStackView
的layout,遍历_UIButtonBarStackView.subViews
得到中间的填充View修改width。
针对两种方式也有不同的实现:
1.利用Runtime替换UIView的layout方法。
2.利用Runtime替换UINavigationItem的setLeftBarButtonItem
,setLeftBarButtonItems
,setRightBarButtonItem
,setRightBarButtonItems
方法。
这两种方法的具体实现代码大家可以去下载demo,默认使用方法1的实现。
大家使用时候只需要将UIViewAdaptor
或者NavigationItemAdaptor
文件夹下的文件导入工程,并且修改对应的space和margin参数即可。