iOS 11 版本由于对于 Nav 层级结构的改变,导致以前的方法无法达到理想的移动效果,使顶部的按钮完全靠左,或者是靠右.

该修改思路目前对于 iOS 11.2 的系统部分情况已经无效.
原因是由于从上一个vc返回到当前vc的时候,可能不会触发当前vc的 viewDidLayoutSubviews 方法,就算触发了此时 self.navigationController.navigationBar 也是空的,导致取不到需要修改约束的 view ,所以造成了约束无法修改为 0 ,达不到现有效果.

临时修改方法

直接写一个 UINavigationBar 的分类,在分类中重写了 layoutSubviews 方法.
重写了之后应该就将原有添加约束的代码覆盖掉了,所以边距不设置也为0了.不过这样的话,内部所有的布局都需要自己完成.

- (void)layoutSubviews{
    for (UIView *subview in self.subviews) {
        if ([NSStringFromClass(subview.class) containsString:@"UIBarBackground"]) {
            if (subview.subviews.count>2) {
                return;
            UIImageView * img=[[UIImageView alloc]initWithFrame:CGRectMake(0, -20, ScreenWidth, 64)];
            img.image=[UIImage imageNamed:@"nav_bg"];
            [subview addSubview:img];

iOS11之前保持原有方式进行设置,iOS11之后进行额外的边距约束修改达到移动效果.

viewDebug的界面上观察可以看到需要将UIButtonBarStackView距离左边和右边的16的约束改为0即可.

图片.png

配置导航器view代码

//0:leftBarButtonItems,1:rightBarButtonItems
- (void)initBarItem:(UIView*)view withType:(int)type{
    UIBarButtonItem * buttonItem = [[UIBarButtonItem alloc]initWithCustomView:view];
    //解决按钮不靠左 靠右的问题.iOS 11系统需要单独处理
    UIBarButtonItem * spaceItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    spaceItem.width = -16;//这个值可以根据自己需要自己调整
    switch (type) {
        case 0:
            if (!IS_IOS_VERSION_11) {
                self.navigationItem.leftBarButtonItems =@[spaceItem,buttonItem];
            }else{
                self.navigationItem.leftBarButtonItems =@[buttonItem];
            break;
        case 1:
            if (!IS_IOS_VERSION_11) {
                self.navigationItem.rightBarButtonItems =@[spaceItem,buttonItem];
            }else{
                self.navigationItem.rightBarButtonItems =@[buttonItem];
            break;
        default:
            break;

处理iOS11情况下的偏移问题,将边距为16的约束的值改为0.

-(void)viewDidLayoutSubviews{
    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) {
            //在plus上这个值为20
            if (fabs(constant.constant)==16) {
                constant.constant=0;

改后效果.png


链接:https://www.jianshu.com/p/8ebc6866ac3e

在设置navigationItemleftBarButtonItem或rightBarButtonItem时, 用CustomView初始化UIBarButtonItem,不论怎么设置CustomView的frame, 添加到导航条上之后总是和屏幕边界有一定的间距(5pix), 如何自由调整这个间距呢?
[code="oc"] UIButton* back = [UIButton buttonWithType:UIButtonTypeCustom]; [back setImage:[UIImage imageNamed:@"Btn1.png"] forState:UIControlStateNormal]; back.frame = CGRectMake(0, 0, 40, ...
IOSUIBarButtonItem * searchBtnItem = [[UIBarButtonItem alloc]initWithCustomView:_searchButton]; UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc] 自定义返回箭头 UIImage *backImage = [[UIImage imageNamed:@"backicon.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; [self.navigationController.navigationBar set...
UINavigationItem - (instancetype)initWithTitle:(NSString *)title NS_DESIGNATED_INITIALIZER; - (nullable instancetype)initWithCoder:(NSCoder *)coder NS_DESIGNATED_INITIALIZER; @property(nullable, non