iOS 導航欄translucent用法


經過導航欄跨越的坑,總結出有兩種方法可以無痕解決(前提>=iOS7版本)(TabBar與導航欄類似)

1、通過設置導航欄的透明度實現(這種方式的控制器view的起始坐標是充(0,64)開始的)

(1)OC實現

  self.navigationBar.translucent = NO;

     設置透明還是非透明:self.navigationController?.navigationBar.translucent = false

(2)swift實現

  navigationBar.isTranslucent = false

 

2、通過控制器edgesForExtendedLayout屬性實現(這種方式的控制器view的起始坐標是充(0,0)開始的),這種方式可以實現導航欄的透明,也可以讓導航欄不透明,具體設置可以根據需求而定,實現透明化用步驟1的代碼即可

如果要實現導航欄和TabBar透明效果下面代碼不能設置

(1)OC實現

  self.edgesForExtendedLayout = UIRectEdgeNone;

(2)swift實現

  edgesForExtendedLayout = .init(rawValue: 0)

3、translucent用法

iOS7之后由於navigationBar.translucent默認是YES,
原點在(0,0)點
當設置NO的時候,原點坐標在(0,64)點

  1.  
    // 原點從(0,64)開始
  2.  
     
  3.  
    self.navigationController.navigationBar.translucent = NO;

5、automaticallyAdjustsScrollViewInsets用法

在用的時候都會有兩種情況咯

1:單獨設置self.automaticallyAdjustsScrollViewInsets

  1.  
    // 原點從(0,64)開始
  2.  
    self.automaticallyAdjustsScrollViewInsets = NO;

2:單獨self.automaticallyAdjustsScrollViewInsets = NO設置,原點就是(0,0)開始

  1.  
    // 原點從(0,0)開始
  2.  
    self.automaticallyAdjustsScrollViewInsets = NO;

3:和self.edgesForExtendedLayout聯合設置,原點就是(0,64)開始

  1.  
    // 原點從(0,64)開始
  2.  
    self.automaticallyAdjustsScrollViewInsets = NO;
  3.  
    self.edgesForExtendedLayout = UIRectEdgeNone;

系統就會自動根據UINavigationBar和statusBar將view下移64,frame從(0,64)開始。這樣,我們在布局內部控件的時候依然可以從(0,0)開始,而不必擔心上部被UINavigationBar遮擋了

3、autoResizingMask的作用

autoResizingMask 是UIView的一個屬性,在一些簡單的布局中,使用autoResizingMask,可以實現子控件相對於父控件的自動布局。

autoResizingMask 是UIViewAutoresizing 類型的,其定義為:

@property(nonatomic) UIViewAutoresizing autoresizingMask;    // simple resize. default is UIViewAutoresizingNone

UIViewAutoresizing 是一個枚舉類型,默認是 UIViewAutoresizingNone,其可以取得值有:

復制代碼
typedef NS_OPTIONS(NSUInteger, UIViewAutoresizing) {
    UIViewAutoresizingNone                 = 0,
    UIViewAutoresizingFlexibleLeftMargin   = 1 << 0,
    UIViewAutoresizingFlexibleWidth        = 1 << 1,
    UIViewAutoresizingFlexibleRightMargin  = 1 << 2,
    UIViewAutoresizingFlexibleTopMargin    = 1 << 3,
    UIViewAutoresizingFlexibleHeight       = 1 << 4,
    UIViewAutoresizingFlexibleBottomMargin = 1 << 5
};
復制代碼

各屬性解釋:

UIViewAutoresizingNone

不會隨父視圖的改變而改變

UIViewAutoresizingFlexibleLeftMargin

自動調整view與父視圖左邊距,以保證右邊距不變

UIViewAutoresizingFlexibleWidth

自動調整view的寬度,保證左邊距和右邊距不變

UIViewAutoresizingFlexibleRightMargin

自動調整view與父視圖右邊距,以保證左邊距不變

UIViewAutoresizingFlexibleTopMargin

自動調整view與父視圖上邊距,以保證下邊距不變

UIViewAutoresizingFlexibleHeight

自動調整view的高度,以保證上邊距和下邊距不變

UIViewAutoresizingFlexibleBottomMargin

自動調整view與父視圖的下邊距,以保證上邊距不變

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM