2017-07-04 54 views
7

我在iOS 11中发现了一个UINavigationBar错误。通过使用self.navigationItem.rightBarButtonItems = @[fixSpaceItem, item]在viewDidLoad中完全设置导航项按钮。并使用手势弹出,但我并没有真正回弹,当回弹开始时,我释放手指并让视图控制器取消弹出,然后右导航按钮项消失。左边的项目按钮有同样的问题。要重现此错误,您必须添加fixSpaceItem,如[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil]。而真正的设备,但不是模拟器可以重现错误。 这里是我的主要代码:如何处理iOS 11导航BarButtonItems错误?

- (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; 
} 
+0

我看到了同样的,即使没有固定的空间。在我的情况下,导航栏会更新到您要弹出的视图控制器的导航项目,但是在取消弹出式中间手势时决不会还原到当前视图控制器导航项目。希望它在下一个测试版中被清除。 –

回答

1

看来当您添加FixedSpace BarButtonItemBarButtonItem 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; 
} 
0

我尝试调整图像的大小以适应它在导航条目内,它工作。尽管您不需要实际调整图像大小,但可以使用下面提供的函数在运行时调整图像大小。

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; 
} 
+0

不要在脚本块中附加Obj C代码 – NSNoob

0
let hamButtonWidthConstraint = hamButton.widthAnchor.constraint(equalToConstant: 40) 

let hamButtonHeightConstraint = hamButton.heightAnchor.constraint(equalToConstant: 40) 

hamButtonWidthConstraint.isActive = true 

hamButtonHeightConstraint.isActive = true