iOS11 UIBarButtonItem:leftBarButtonItem/rightBarButtonItem文字长度太长解决方案
6EAE84E0-609D-45A4-87CE-398590771536.png查看nav 的导航的层级关系
5F2D6A85-E71C-4AF2-A550-903E7A5420BB.pngleftBarButtonItem/rightBarButtonItem位置位于<_UIButtonBarStackView: 0x7fad96c09cf0; frame = (16 0; 351 44); layer = <CALayer: 0x6000000344c0>>
_UIButtonBarStackView 上边
leftBarButtonItem/rightBarButtonItem在iOS11存在UIStackView上
下边稍微解释一下UIStackView这个控件
‘Stack View 核心是方便垂直或水平排布多个SubView
Stack View 会为每个SubView创建和添加Auto Layout constrains,也可以自己控制SubView的大小和位置
得出结论Stack View 和他的每一个字控件的布局关心是约束关系,而不是frame
解决方案
添加约束确定leftBarButtonItem/rightBarButtonItem button 的大小
创建的button 的代码来源于 《花花》
NSString *backStr = @"backStrbackStrbackStrbackStrbackStr";
UIImage *barItemImg = [UIImage imageNamed:@""];
UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
backButton.frame = CGRectMake(0, 0, 40, 40);
[backButton setImage:barItemImg forState:UIControlStateNormal];
if (backStr.length != 0)
{
NSDictionary *attribute = @{NSFontAttributeName: [UIFont systemFontOfSize:15]};
CGSize textSize = [backStr boundingRectWithSize:CGSizeMake((375-150), 44) options:NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:attribute context:nil].size;
if (textSize.width > 50)
{
textSize.width = 50;
}
backButton.titleLabel.font = [UIFont systemFontOfSize:15];
backButton.frame = CGRectMake(0, 0, barItemImg.size.width+textSize.width+10, 40);
[backButton setTitle:backStr forState:UIControlStateNormal];
backButton.titleLabel.lineBreakMode =NSLineBreakByTruncatingTail;
backButton.backgroundColor = [UIColor redColor];
backButton.imageEdgeInsets = UIEdgeInsetsMake(10, 0, 10, textSize.width+5);
//button标题的偏移量,这个偏移量是相对于图片的
backButton.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
[backButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
}else
{
backButton.imageEdgeInsets = UIEdgeInsetsMake(0, -10, 0, 10);
}
backButton.frame = CGRectMake(0, 0, 40, 40);
[backButton addTarget:self action:@selector(popLeftItemView) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
[self.navigationItem setLeftBarButtonItems:@[backItem,space]];
添加约束代码
NSLayoutConstraint *heightConstraint = [NSLayoutConstraint constraintWithItem:self.navigationItem.leftBarButtonItem.customView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:0.0 constant:50];
[self.navigationItem.leftBarButtonItem.customView addConstraint:heightConstraint];
NSLayoutConstraint *heightConstraintWidth = [NSLayoutConstraint constraintWithItem:self.navigationItem.leftBarButtonItem.customView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:0.0 constant:50];
[self.navigationItem.leftBarButtonItem.customView addConstraint:heightConstraint];
[self.navigationItem.leftBarButtonItem.customView addConstraint:heightConstraintWidth];
效果图
6844777F-1C1D-40AA-ACCB-4324CFE4121A.png
网友评论