iOS 11 導航欄問題

前言

iOS11導航欄除了新加入了largeTitles和searchController兩個新特性,可能是加入largeTitles的原因其結(jié)構(gòu)較iOS 10發(fā)生了些變化。

iOS11之前導航欄的navigationBarButton則直接添加在navigationBar上面


image.png

在iOS11之后,蘋果添加了新的類來管理,可以看到titleView直接加在_UINavigationBarContentView上,UIBarButtonItem則添加在_UIButtonBarStackView上面,而_UIButtonBarStackView則添加在_UINavigationBarContentView上面,最后添加到UINavigationBar上面,如下圖所示:


image.png

由于結(jié)構(gòu)的變化,在iOS 11中我們自定義設(shè)置leftBarButtonItem,其點擊區(qū)域變得很小,讓人點的很焦灼,如下圖綠色區(qū)域所示:

image.png

具體代碼如下,設(shè)置的frame在這里并沒有什么卵用,點擊區(qū)域依然只有圖片原本的size那么大:

       UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 60, 40)];
       [btn setImage:imageWhite forState:UIControlStateNormal];
       [btn addTarget:self action:@selector(bpBack) forControlEvents:UIControlEventTouchUpInside];
       btn.backgroundColor = [UIColor greenColor];
       UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
       leftItem.width = 60;
       self.navigationItem.leftBarButtonItem = leftItem;

為了能增加點擊區(qū)域,我們就需要增加button的size,然后就想到通過改變ContentEdgeInsets來增大button的size,

... 
...
 btn.backgroundColor = [UIColor greenColor];
  if (@available(iOS 11.0,*)) {
          [btn setContentMode:UIViewContentModeScaleToFill];
          [btn setContentEdgeInsets:UIEdgeInsetsMake(0, 5, 5, 20)];
   }
  UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
...
...

另:searchBar設(shè)置為titleview,會導致navigation的高度發(fā)生異常(ps:push到下一個界面,下個界面的view距離navigation出現(xiàn)了一段黑色區(qū)域)需要處理下:

   CGRect frame = CGRectMake(0, 0, 150, 44);
    UISearchBar *search = [[UISearchBar alloc] initWithFrame:frame];
    search.placeholder = @"搜索";
    search.delegate = self;
    UITextField *searchField=[search valueForKey:@"_searchField"];
    searchField.backgroundColor = [UIColor groupTableViewBackgroundColor];
// --- iOS 11異常處理
    if(@available(iOS 11.0, *)) {
        [[search.heightAnchor constraintEqualToConstant:44] setActive:YES];
    }
    self.navigationItem.titleView = search;

詳細資料參考:
https://stackoverflow.com/questions/45997996/ios-11-uisearchbar-in-uinavigationbar

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,221評論 4 61
  • iOS 11適配源碼 Demo地址 安全區(qū)域的適配 用Xcode 9 創(chuàng)建storyboard或者xib時,最低版...
    Bytesking閱讀 6,745評論 5 24
  • 我愛你
    毀滅閱讀 311評論 0 0
  • 二月春意東風晚 街柳吐芽卻不遲
    天馬行空a閱讀 189評論 0 0

友情鏈接更多精彩內(nèi)容