新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
iOS中UINavigationItem如何去除左右間隙-創(chuàng)新互聯(lián)
小編給大家分享一下iOS中UINavigationItem如何去除左右間隙,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!
修改思路
在iOS11之前保持原有方式進行設置,iOS11之后進行額外的邊距約束修改達到移動效果.
從viewDebug的界面上觀察可以看到需要將UIButtonBarStackView距離左邊和右邊的16的約束改為0即可.
核心代碼
配置導航器view代碼
//0:leftBarButtonItems,1:rightBarButtonItems - (void)initBarItem:(UIView*)view withType:(int)type{ UIBarButtonItem * buttonItem = [[UIBarButtonItem alloc]initWithCustomView:view]; //解決按鈕不靠左 靠右的問題.iOS 11系統(tǒng)需要單獨處理 UIBarButtonItem * spaceItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil]; spaceItem.width = -16;//這個值可以根據(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){ //這里需要注意,你設置的第一個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如何去除左右間隙”有了一定的了解,如果想了解更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)網(wǎng)站建設公司行業(yè)資訊頻道,感謝各位的閱讀!
另外有需要云服務器可以了解下創(chuàng)新互聯(lián)建站www.cdcxhl.com,海內(nèi)外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。
分享文章:iOS中UINavigationItem如何去除左右間隙-創(chuàng)新互聯(lián)
文章源于:http://www.dlmjj.cn/article/dpcehg.html