iOS11NavigationItem偏移,iOS11适配问题,iOS11导航栏返回偏移,iOS11BarButtonItem偏移,Xcode9遇见的问题

更新iOS 11之后,用xcode 9运行App,你会发现以下问题:

注意:此文章代码做了更新,对于item点击不灵敏问题,文章结尾写出了解决办法。

1、MJ刷新异常

2、tableView的section之间间距变大,空白区域

3、导航栏返回按钮偏移20像素

下面我逐个讲一下解决办法:

1、MJ刷新异常,上拉加载出现跳动刷新问题:MJ已经更新,解决了此bug

2、tableView的section之间间距变大问题:

解决办法:初始化的时候增加以下代码

     self.tableView.estimatedRowHeight =0;

     self.tableView.estimatedSectionHeaderHeight =0;

     self.tableView.estimatedSectionFooterHeight =0;


3、导航栏按钮偏移20像素问题:

解决办法:

楼主写的分类:UIViewController+BarButton

代码如下:

//左侧一个图片按钮的情况

- (void)addLeftBarButtonWithImage:(UIImage *)image action:(SEL)action

{

    UIView *view = [[UIViewalloc]initWithFrame:CGRectMake(0,0,44,44)];

    view.backgroundColor = [UIColorclearColor];

    

    UIButton *firstButton = [UIButtonbuttonWithType:UIButtonTypeCustom];

    firstButton.frame = CGRectMake(0, 0, 44, 44);

    [firstButton setImage:imageforState:UIControlStateNormal];

    [firstButton addTarget:selfaction:actionforControlEvents:UIControlEventTouchUpInside];

    


        firstButton.contentHorizontalAlignment =UIControlContentHorizontalAlignmentLeft;

        [firstButton setImageEdgeInsets:UIEdgeInsetsMake(0,5 *kScreenWidth /375.0,0,0)];


    

    UIBarButtonItem *leftBarButtonItem = [[UIBarButtonItemalloc]initWithCustomView:firstButton];


    self.navigationItem.leftBarButtonItem = leftBarButtonItem;

}


//右侧一个图片按钮的情况

- (void)addRightBarButtonWithFirstImage:(UIImage *)firstImage action:(SEL)action

{

    UIView *view = [[UIViewalloc]initWithFrame:CGRectMake(0,0,44,44)];

    view.backgroundColor = [UIColorclearColor];

    

    UIButton *firstButton = [UIButtonbuttonWithType:UIButtonTypeCustom];

    firstButton.frame = CGRectMake(0, 0, 44, 44);

    [firstButton setImage:firstImageforState:UIControlStateNormal];

    [firstButton addTarget:selfaction:actionforControlEvents:UIControlEventTouchUpInside];

    

    

        firstButton.contentHorizontalAlignment =UIControlContentHorizontalAlignmentRight;

        [firstButton setImageEdgeInsets:UIEdgeInsetsMake(0,0,0,5 *kScreenWidth /375.0)];

    

    

    UIBarButtonItem *rightBarButtonItem = [[UIBarButtonItemalloc]initWithCustomView:firstButton];


    self.navigationItem.rightBarButtonItem = rightBarButtonItem;

}

//右侧为文字item的情况

- (void)addRightBarButtonItemWithTitle:(NSString *)itemTitle action:(SEL)action

{


    UIButton *rightbBarButton = [[UIButtonalloc]initWithFrame:CGRectMake(0,0,88,44)];

    [rightbBarButton setTitle:itemTitleforState:(UIControlStateNormal)];

    [rightbBarButton setTitleColor:kDarkOneColorforState:(UIControlStateNormal)];

    rightbBarButton.titleLabel.font = [UIFontsystemFontOfSize:14];

    [rightbBarButton addTarget:selfaction:actionforControlEvents:(UIControlEventTouchUpInside)];

    

        rightbBarButton.contentHorizontalAlignment =UIControlContentHorizontalAlignmentRight;

        [rightbBarButton setTitleEdgeInsets:UIEdgeInsetsMake(0,0,0,5 *kScreenWidth/375.0)];

    

    

    self.navigationItem.rightBarButtonItem = [[UIBarButtonItemalloc]initWithCustomView:rightbBarButton];

}

//左侧为文字item的情况

- (void)addLeftBarButtonItemWithTitle:(NSString *)itemTitle action:(SEL)action

{

    UIButton *leftbBarButton = [[UIButtonalloc]initWithFrame:CGRectMake(0,0,44,44)];

    [leftbBarButton setTitle:itemTitleforState:(UIControlStateNormal)];

    [leftbBarButton setTitleColor:kDarkOneColorforState:(UIControlStateNormal)];

    leftbBarButton.titleLabel.font = [UIFontsystemFontOfSize:16];

    [leftbBarButton addTarget:selfaction:actionforControlEvents:(UIControlEventTouchUpInside)];

    

        leftbBarButton.contentHorizontalAlignment =UIControlContentHorizontalAlignmentLeft;

        [leftbBarButton setTitleEdgeInsets:UIEdgeInsetsMake(0,5 *kScreenWidth/375.0,0,0)];

    

    

    self.navigationItem.leftBarButtonItem = [[UIBarButtonItemalloc]initWithCustomView:leftbBarButton];

}


//右侧两个图片item的情况

- (void)addRightTwoBarButtonsWithFirstImage:(UIImage *)firstImage firstAction:(SEL)firstAction secondImage:(UIImage *)secondImage secondAction:(SEL)secondAction

{

    UIView *view = [[UIViewalloc]initWithFrame:CGRectMake(0,0,80,44)];

    view.backgroundColor = [UIColorclearColor];

    

    UIButton *firstButton = [UIButtonbuttonWithType:UIButtonTypeCustom];

    firstButton.frame = CGRectMake(40, 0, 40, 44);

    [firstButton setImage:firstImageforState:UIControlStateNormal];

    [firstButton addTarget:selfaction:firstActionforControlEvents:UIControlEventTouchUpInside];

    

        firstButton.contentHorizontalAlignment =UIControlContentHorizontalAlignmentRight;

        [firstButton setImageEdgeInsets:UIEdgeInsetsMake(0,0,0,5 *kScreenWidth/375.0)];

    

    [view addSubview:firstButton];

    

    UIButton *secondButton = [UIButtonbuttonWithType:UIButtonTypeCustom];

    secondButton.frame = CGRectMake(0, 0, 40, 44);

    [secondButton setImage:secondImageforState:UIControlStateNormal];

    [secondButton addTarget:selfaction:secondActionforControlEvents:UIControlEventTouchUpInside];

    

        secondButton.contentHorizontalAlignment =UIControlContentHorizontalAlignmentRight;

        [secondButton setImageEdgeInsets:UIEdgeInsetsMake(0,0,0,0 *kScreenWidth/375.0)];

    

    [view addSubview:secondButton];

    

    UIBarButtonItem *rightBarButtonItem = [[UIBarButtonItemalloc]initWithCustomView:view];


    self.navigationItem.rightBarButtonItem = rightBarButtonItem;

}

//右侧三个图片item的情况

- (void)addRightThreeBarButtonsWithFirstImage:(UIImage *)firstImage firstAction:(SEL)firstAction secondImage:(UIImage *)secondImage secondAction:(SEL)secondAction thirdImage:(UIImage *)thirdImage thirdAction:(SEL)thirdAction

{

    UIView *view = [[UIViewalloc]initWithFrame:CGRectMake(0,0,120,44)];

    view.backgroundColor = [UIColorclearColor];

    

    UIButton *firstButton = [UIButtonbuttonWithType:UIButtonTypeCustom];

    firstButton.frame = CGRectMake(80, 0, 40, 44);

    [firstButton setImage:firstImageforState:UIControlStateNormal];

    [firstButton addTarget:selfaction:firstActionforControlEvents:UIControlEventTouchUpInside];

    

        firstButton.contentHorizontalAlignment =UIControlContentHorizontalAlignmentRight;

        [firstButton setImageEdgeInsets:UIEdgeInsetsMake(0,0,0,5 *kScreenWidth/375.0)];

    

    [view addSubview:firstButton];

    

    UIButton *secondButton = [UIButtonbuttonWithType:UIButtonTypeCustom];

    secondButton.frame = CGRectMake(44, 0, 40, 44);

    [secondButton setImage:secondImageforState:UIControlStateNormal];

    [secondButton addTarget:selfaction:secondActionforControlEvents:UIControlEventTouchUpInside];

    

        secondButton.contentHorizontalAlignment =UIControlContentHorizontalAlignmentRight;

        [secondButton setImageEdgeInsets:UIEdgeInsetsMake(0,0,0,8 *kScreenWidth/375.0)];

    

    [view addSubview:secondButton];

    

    UIButton *thirdButton = [UIButtonbuttonWithType:UIButtonTypeCustom];

    thirdButton.frame = CGRectMake(0, 0, 40, 44);

    [thirdButton setImage:thirdImageforState:UIControlStateNormal];

    [thirdButton addTarget:selfaction:thirdActionforControlEvents:UIControlEventTouchUpInside];

    

    [view addSubview:thirdButton];

    

    self.navigationItem.rightBarButtonItem = [[UIBarButtonItemalloc]initWithCustomView:view];

}



在你的控制器里边,直接[self addleft...]就可以调用,如果觉得坐标有问题,可以自行处理变更

如果你的需求是三个四个item按钮,可以仿照上边两个item按钮的方法,自行处理。

有问题请留言,我看到后会及时处理。

注意:以上我写的方法,已经不会偏移了。

但是会出现点击不到item情况。借鉴了UINavigation-SXFixSpace的几个分类,解决了我的问题。谢谢此位大哥的分享。

点击下载demo

猜你喜欢

转载自blog.csdn.net/zhaotao0617/article/details/78063485