美文网首页
调整导航条上leftBarButtonItem和rightBar

调整导航条上leftBarButtonItem和rightBar

作者: Desert_Eagle | 来源:发表于2017-07-13 17:27 被阅读0次

    1、iOS11以下设置navigationItem的边距

    UIView *backView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 44, 44)];
        backView.backgroundColor = [UIColor magentaColor];
        UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithCustomView:backView];
        // 用于控制间距的item
        UIBarButtonItem *spaceItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
        /**
         *  width为负数时相当于向左移动width数值的距离,为正数时相当于向右移动width数值的距离
         *  自己测了下,左边应该是16px,右边应该是12px
         */
        spaceItem.width = -15;
        self.navigationItem.leftBarButtonItems = @[spaceItem,backItem];
    

    2、iOS设置navigationItem的边距

    - (void)viewDidLoad {
        [super viewDidLoad];
        
        UIView *backView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 44, 44)];
        
        UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
        backButton.frame = CGRectMake(0, 0, 44, 44);
        [backButton setImage:[UIImage imageNamed:@"navigation_back"] forState:UIControlStateNormal];
        [backButton addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];
        [backView addSubview:backButton];
        
        if ([UIDevice currentDevice].systemVersion.floatValue >= 11.0) {
            self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backView];
        } else {
            UIBarButtonItem *spaceItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
            spaceItem.width = -15;
            UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithCustomView:backView];
            self.navigationItem.leftBarButtonItems = @[spaceItem,backItem];
        }
    }
    
    - (void)viewDidLayoutSubviews{
        if ([UIDevice currentDevice].systemVersion.floatValue >= 11.0) {
            UINavigationItem *item = self.navigationItem;
            NSArray *array = item.leftBarButtonItems;
            if (array && array.count) {
                // 注意:这里设置的第一个leftBarButtonItem的customeView不能为空,也就是不要设置UIBarButtonSystemItemFixedSpace这种风格的item
                UIBarButtonItem *btnItem = array[0];
                UIView *view = [[btnItem.customView.superview superview] superview];
                NSArray *arrayConstraint = view.constraints;
                for (NSLayoutConstraint *constant in arrayConstraint) {
                    CGFloat space = fabs(constant.constant);
                    // 非plus手机为16 plus手机为20
                    if (space == 16 || space == 20) {
                        constant.constant = 0;
                    }
                }
            }
        }
    }
    

    相关文章

      网友评论

          本文标题:调整导航条上leftBarButtonItem和rightBar

          本文链接:https://www.haomeiwen.com/subject/xgfghxtx.html