溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

iOS中UINavigationItem如何去除左右間隙

發(fā)布時(shí)間:2021-07-12 11:09:24 來(lái)源:億速云 閱讀:292 作者:小新 欄目:移動(dòng)開(kāi)發(fā)

小編給大家分享一下iOS中UINavigationItem如何去除左右間隙,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!

修改思路

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

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

iOS中UINavigationItem如何去除左右間隙

核心代碼

配置導(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;
      }
    }
  }
}

iOS中UINavigationItem如何去除左右間隙

看完了這篇文章,相信你對(duì)“iOS中UINavigationItem如何去除左右間隙”有了一定的了解,如果想了解更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI