我在iOS 11中发现了一个UINavigationBar错误。用self.navigationItem.rightBarButtonItems = @[fixSpaceItem, item]在viewDidLoad中快速设置导航项目按钮。使用手势弹出,但我并不是真的弹回,当弹回开始时,我松开手指,让视图控制器取消弹出,然后右导航按钮项目消失。左边的项目按钮也有同样的问题。要重现此错误,您必须添加类似于[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil]的fixSpaceItem。而真实的设备而不是模拟器可以重现bug。下面是我的主要代码:
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationController.interactivePopGestureRecognizer.enabled = YES;
self.navigationController.interactivePopGestureRecognizer.delegate = self;
self.navigationItem.rightBarButtonItems = @[[self negativeSpacerWithWidth:5],[self rightButton]];
self.navigationItem.leftBarButtonItems = @[[self negativeSpacerWithWidth:5], [self leftButton]];
}
- (UIBarButtonItem *)leftButton {
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 35, 35)];
[button setImage:[UIImage imageNamed:@"icon_app_back_normal"] forState:UIControlStateNormal];
[button addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:button];
return item;
}
- (UIBarButtonItem *)rightButton {
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 35, 35)];
[button setImage:[UIImage imageNamed:@"setting"] forState:UIControlStateNormal];
UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:button];
return item;
}
- (UIBarButtonItem *)negativeSpacerWithWidth:(CGFloat)width {
UIBarButtonItem *spacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
[spacer setWidth:width];
return spacer;
}发布于 2017-07-04 12:02:16
当您将FixedSpace BarButtonItem添加到BarButtonItem array时,它似乎是一个错误。如果你想设置导航项目的偏移量,可能不得不使用另一种方式,例如更改按钮的imageEdgeInsets。
- (void)viewDidLoad {
[super viewDidLoad];
// Do not set Fixed Space type button item.
self.navigationItem.leftBarButtonItem = [self leftButton];
self.navigationItem.rightBarButtonItem = [self rightButton];
// It work too
//self.navigationItem.leftBarButtonItems = @[[self leftButton], [self leftButton]];
//self.navigationItem.rightBarButtonItems = @[[self rightButton], [self rightButton]];
self.navigationController.interactivePopGestureRecognizer.enabled = YES;
self.navigationController.interactivePopGestureRecognizer.delegate = self;
}
- (UIBarButtonItem *)leftButton
{
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 35, 35)];
//...
// to add offset you want
button.imageEdgeInsets = UIEdgeInsetsMake(0, -15, 0, 15);
UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:button];
return item;
}
- (UIBarButtonItem *)rightButton
{
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 35, 35)];
//...
// to add offset you want
button.imageEdgeInsets = UIEdgeInsetsMake(0, 15, 0, -15);
UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:button];
return item;
}发布于 2017-09-22 13:52:22
我试着调整图像的大小,使其适合导航项目,并成功了。虽然您不需要实际调整图像的大小,但您可以使用下面提供的函数在运行时调整图像的大小。
UIImage *imgCart = [self imageWithImage:[UIImage imageNamed:@"ic_cart"] scaledToSize:CGSizeMake(35, 35)] ;
UIButton *btnCart = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
[btnCart addTarget:self action:@selector(btnCartClicked:) forControlEvents:UIControlEventTouchUpInside];
[btnCart setBackgroundImage:imgCart forState:UIControlStateNormal];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btnCart];
-(UIImage *)imageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize {
//UIGraphicsBeginImageContext(newSize);
// In next line, pass 0.0 to use the current device's pixel scaling factor (and thus account for Retina resolution).
// Pass 1.0 to force exact pixel size.
UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
[image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}发布于 2017-09-27 14:19:06
let hamButtonWidthConstraint = hamButton.widthAnchor.constraint(equalToConstant: 40)
let hamButtonHeightConstraint = hamButton.heightAnchor.constraint(equalToConstant: 40)
hamButtonWidthConstraint.isActive = true
hamButtonHeightConstraint.isActive = truehttps://stackoverflow.com/questions/44896043
复制相似问题