iOS 11 UINavigationItem 去除左右間隙的方法
前言
iOS 11版本由于對(duì)于Nav層級(jí)結(jié)構(gòu)的改變,導(dǎo)致以前的方法無法達(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]; //解決按鈕不靠左 靠右的問題.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情況下的偏移問題,將邊距為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
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
iOS中UIScrollerView的用法及基于AotoLayout的控件懸停
這篇文章主要介紹了iOS中UIScrollerView的用法及基于AotoLayout的控件懸停,文中對(duì)于UIScrollerView的方法及屬性介紹地非常詳細(xì),十分推薦,示例代碼為Objective-C,需要的朋友可以參考下2016-03-03iOS中關(guān)于Swift UICollectionView橫向分頁的問題
這篇文章通過圖文并茂的形式給大家介紹UICollectionView橫向分頁的問題,非常不錯(cuò),具有參考借鑒價(jià)值,需要的的朋友參考下吧2017-05-05IOS 基礎(chǔ)之設(shè)置 tableview 的分割線
這篇文章主要介紹了IOS 基礎(chǔ)之設(shè)置 tableview 的分割線的相關(guān)資料,需要的朋友可以參考下2017-03-03iOS如何去掉導(dǎo)航欄(UINavigationBar)下方的橫線
本篇文章主要介紹了iOS如何去掉導(dǎo)航欄(UINavigationBar)下方的橫線,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2017-05-05