我注意到iOS 11对UIBarButtonItem进行了一些更改。在解决了UIBarButtonItem图像的大小问题之后,我发现自己面临着另一个更奇怪的问题。
我们有一个带有几个UIBarButtonItem的UIToolBar,我们习惯将UIBarButtonItem的宽度设置为40,将UIButton.image的宽度设置为24,这样每两个UIBarButtonItem之间留有一个很好的间距。但是,在iOS 11中,空间消失了。
我试过
[self.deleteButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.size.mas_equalTo(CGSizeMake(40, 24));
}];
NSLayoutConstraint *w = [self.deleteButton.imageView.widthAnchor constraintEqualToConstant:24];
NSLayoutConstraint *h = [self.deleteButton.imageView.widthAnchor constraintEqualToConstant:24];
w.active = YES;
h.active = YES;
但它并没有像我想象的那样奏效。
我要么用CGSize(40,24)得到一个拉伸图像列表,要么得到一个带有CGSize(24,24)的UIBarButtonItem列表,这些列表在UINavigationBar中逐个排列,没有间距。
我是否需要添加任何其他约束来创建间距?
答案 0 :(得分:1)
试试这个:
UIButton*(^buttonWith)(NSString *) = ^(NSString *imageName) {
CGFloat size = 40.0;
UIButton *button = [[UIButton alloc] initWithFrame: CGRectMake(0.0, 0.0, size, size)];
[button setImage: [[UIImage imageNamed: imageName] imageWithRenderingMode: UIImageRenderingModeAlwaysOriginal] forState: UIControlStateNormal];
[button addConstraint: [NSLayoutConstraint constraintWithItem: button attribute: NSLayoutAttributeWidth relatedBy: NSLayoutRelationEqual toItem: nil attribute: NSLayoutAttributeNotAnAttribute multiplier: 1.0 constant: size]];
[button addConstraint: [NSLayoutConstraint constraintWithItem: button attribute: NSLayoutAttributeHeight relatedBy: NSLayoutRelationEqual toItem: nil attribute: NSLayoutAttributeNotAnAttribute multiplier: 1.0 constant: size]];
return button;
};
用法:
UIButton *resetButton = buttonWith(@"reset");
self.resetBarButton = [[UIBarButtonItem alloc] initWithCustomView: resetButton];
UIButton *backButton = buttonWith(@"back");
self.backBarButton = [[UIBarButtonItem alloc] initWithCustomView: backButton];
self.navigationItem.leftBarButtonItems = @[self.backBarButton, self.resetBarButton];
答案 1 :(得分:0)