ios 11导航栏返回按钮间距问题

 if (!IS_IOS_VERSION_11) return;
    UINavigationItem * item=self.navigationItem;
    NSArray * array=item.leftBarButtonItems;//根据你的是左按钮还是右按钮
    if (array&&array.count!=0){
        //这里需要注意,你设置的第一个leftBarButtonItem的customeView不能是空的,也就是不要设置UIBarButtonSystemItemFixedSpace这种风格的item
        UIBarButtonItem * buttonItem=array[0];
        UIView * view =[[[buttonItem.customView superview] superview] superview];
        NSArray * arrayConstraint=view.constraints;
        for (NSLayoutConstraint * constant in arrayConstraint) {
            if (fabs(constant.constant)==16) {
                constant.constant=0;
            }
        }
在这三个周期里面都得写

-(void)viewDidLayoutSubviews{
    
    if (!IS_IOS_VERSION_11) return;
    UINavigationItem * item=self.navigationItem;
    NSArray * array=item.leftBarButtonItems;
    if (array&&array.count!=0){
        UIBarButtonItem * buttonItem=array[0];
        UIView * view =[[[buttonItem.customView superview] superview] superview];
        NSArray * arrayConstraint=view.constraints;
        for (NSLayoutConstraint * constant in arrayConstraint) {
            if (fabs(constant.constant)==16) {
                constant.constant=0;
            }
        }
    }
 
}
-(void)viewWillAppear:(BOOL)animated{
    
      [super viewWillAppear:animated];
    if (!IS_IOS_VERSION_11) return;
    UINavigationItem * item=self.navigationItem;
    NSArray * array=item.leftBarButtonItems;
    if (array&&array.count!=0){
        UIBarButtonItem * buttonItem=array[0];
        UIView * view =[[[buttonItem.customView superview] superview] superview];
        NSArray * arrayConstraint=view.constraints;
        
        for (NSLayoutConstraint * constant in arrayConstraint) {
            if (fabs(constant.constant)==16) {
                constant.constant=0;
            }
        }
    }
    
}

//防止侧滑出现问题
- (void)viewDidAppear:(BOOL)animated{
    
    [super viewDidAppear:animated];
    if (!IS_IOS_VERSION_11) return;
    UINavigationItem * item=self.navigationItem;
    NSArray * array=item.leftBarButtonItems;
    if (array&&array.count!=0){
        UIBarButtonItem * buttonItem=array[0];
        UIView * view =[[[buttonItem.customView superview] superview] superview];
        NSArray * arrayConstraint=view.constraints;
        
        for (NSLayoutConstraint * constant in arrayConstraint) {
            if (fabs(constant.constant)==16) {
                constant.constant=0;
            }
        }
    }
    
}

你可能感兴趣的:(ios 11导航栏返回按钮间距问题)