欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

iOS 11 UINavigationItem 去除左右間隙的方法

 更新時(shí)間:2017年10月20日 14:07:58   作者:stonemover  
本篇文章主要介紹了iOS 11 UINavigationItem 去除左右間隙的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧

前言

iOS 11版本由于對(duì)于Nav層級(jí)結(jié)構(gòu)的改變,導(dǎo)致以前的方法無(wú)法達(dá)到理想的移動(dòng)效果,使頂部的按鈕完全靠左,或者是靠右.

修改思路

在iOS11之前保持原有方式進(jìn)行設(shè)置,iOS11之后進(jìn)行額外的邊距約束修改達(dá)到移動(dòng)效果.

從viewDebug的界面上觀察可以看到需要將UIButtonBarStackView距離左邊和右邊的16的約束改為0即可.


核心代碼

配置導(dǎo)航器view代碼

//0:leftBarButtonItems,1:rightBarButtonItems
- (void)initBarItem:(UIView*)view withType:(int)type{
  UIBarButtonItem * buttonItem = [[UIBarButtonItem alloc]initWithCustomView:view];
  //解決按鈕不靠左 靠右的問(wèn)題.iOS 11系統(tǒng)需要單獨(dú)處理
  UIBarButtonItem * spaceItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
  spaceItem.width = -16;//這個(gè)值可以根據(jù)自己需要自己調(diào)整
  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情況下的偏移問(wèn)題,將邊距為16的約束的值改為0.

-(void)viewDidLayoutSubviews{
  if (!IS_IOS_VERSION_11) return;
  UINavigationItem * item=self.navigationItem;
  NSArray * array=item.leftBarButtonItems;
  if (array&&array.count!=0){
    //這里需要注意,你設(shè)置的第一個(gè)leftBarButtonItem的customeView不能是空的,也就是不要設(shè)置UIBarButtonSystemItemFixedSpace這種風(fēng)格的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;
      }
    }
  }
}

改后效果.png

Demo地址:https://github.com/StoneMover/navDemo.git

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論