扩大UIBarButtonItem的点击区域,不影响之前的布局

效果如下:

old.png
new.png

设置思路:
1.UINavigationBar代码

if (iOS11_Later) {
    
    self.layoutMargins = UIEdgeInsetsZero;
    
    for (UIView *subview in self.subviews) {
        
        if ([NSStringFromClass(subview.class) containsString:@"ContentView"]) {
            subview.layoutMargins = UIEdgeInsetsZero;//可修正iOS11之后的偏移
        }
    }
}

2.UINavigationItem代码

    if ([item.customView isKindOfClass:[UIButton class]]){
        
        UIButton *leftBtn = item.customView;
        if (!item.fixedState) {
            
            UIEdgeInsets earlyEdge = leftBtn.contentEdgeInsets;
            earlyEdge.left += 15;
            leftBtn.width += 15;
            leftBtn.contentEdgeInsets = earlyEdge;
            item.fixedState = @"setted";
        }
        
    }else{
        
        UIBarButtonItem *fixBtn = [self fixedSpaceWithWidth:15];
        [self JR_setLeftBarButtonItems:@[fixBtn,item]];
        return;
    }

Demo地址:https://github.com/ycl1987/YCLBarButtonItem

你可能感兴趣的:(扩大UIBarButtonItem的点击区域,不影响之前的布局)