iOS中UINavigationItem如何去除左右间隙-成都创新互联网站建设

关于创新互联

多方位宣传企业产品与服务 突出企业形象

公司简介 公司的服务 荣誉资质 新闻动态 联系我们

iOS中UINavigationItem如何去除左右间隙

小编给大家分享一下iOS中UINavigationItem如何去除左右间隙,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!

10年积累的网站设计、成都网站制作经验,可以快速应对客户对网站的新想法和需求。提供各种问题对应的解决方案。让选择我们的客户得到更好、更有力的网络服务。我虽然不认识你,你也不认识我。但先网站设计后付款的网站建设流程,更有淇滨免费网站建设让你可以放心的选择与我们合作。

修改思路

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

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

iOS中UINavigationItem如何去除左右间隙

核心代码

配置导航器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) {
      if (fabs(constant.constant)==16) {
        constant.constant=0;
      }
    }
  }
}

iOS中UINavigationItem如何去除左右间隙

看完了这篇文章,相信你对“iOS中UINavigationItem如何去除左右间隙”有了一定的了解,如果想了解更多相关知识,欢迎关注创新互联行业资讯频道,感谢各位的阅读!


网页名称:iOS中UINavigationItem如何去除左右间隙
网站URL:http://kswsj.cn/article/jecghi.html

其他资讯