translucent
這個BOOL屬性能控制 UITabBar/UINavigationBar 的半透明效果,默認為YES,即默認情況下為半透明效果。
1. 設置translucent值對 對應的 UITabBar/UINavigationBar 設置圖片的影響
translucent(默認為YES)
- NO:強制使用非透明背景
- 如果導航條使用自定義背景圖片,那么默認情況該屬性的值由圖片的alpha(透明度)決定,如果alpha的透明度小于1.0值為YES。
- 使用自定義帶透明度(透明度小于0)的圖片,那么系統會展示這張背景圖片,只不過這張圖片會使用事先確定的barTintColor進行不透明處理,若barTintColor為空,則會使用UIBarStyleBlack(黑色)或者UIBarStyleDefault(白色)。
- YES:默認使用原生半透明效果
- 如果導航條使用自定義不透明圖片,那么會自動設置系統透明度(小于1.0)在這個圖片上。
ps:設置導航欄背景圖片透明度問題
- 如果背景圖片沒有透明度,系統會自動把導航控制器的棧頂控制器的view的Y值增加64,如果有透明度,則不會增加。
- 這一情況的圖片必須是imageSet格式的,并且圖片的透明度為1(不透明),系統才會去調整。
2. 設置translucent值對 控制器 View、普通View 、scrollView 布局的影響
控制器 View、普通View布局影響
-
translucent 屬性默認為 YES (保留半透明效果)
默認情況下,如果使用 UITabBarController 和 UINavigationController(translucent屬性默認為YES),在其子控制器的 View添加一個藍色的 view 并設置距離屏幕邊距為(0,0,0,0),展示效果如下:
- translucent 屬性默認為 NO (不保留半透明效果)
導航條的translucent設置為NO效果展示 | TabBar的translucent設置為NO效果展示 |
---|---|
導航條的translucent設置為NO效果展示.png
|
TabBar的translucent設置為NO效果展示.png
|
對 ScrollView 布局的影響
- 將上述藍色的 view 替換成 tableView,展示效果如下:
tableView默認情況示例1 | tableView默認情況示例2.png |
---|---|
tableView默認情況示例1.png
|
tableView默認情況示例2.png
|
tableView 的 cell 并沒有因為“穿透”效果而出現被遮擋的情況,這是由于蘋果對滾動視圖的特殊性進行處理:對于類ScrollView,系統默認默認控制器屬性automaticallyAdjustsScrollViewInsets默認為YES。
對于普通 scrollView 默認情況 translucent = YES && automaticallyAdjustsScrollViewInsets = YES,scrollView 的內容會在 navBar 底部開始到 TabBar 頂部結束
請注意:iOS11開始,蘋果摒棄了ViewController 的
automaticallyAdjustsScrollViewInsets屬性,改由ScrollView 的
contentInsetAdjustmentBehavior(枚舉值)控制,下面會有詳細的解釋。
-
automaticallyAdjustsScrollViewInsets = YES
時系統底層所干的事scrollView的內容原本沒有內邊距,但是考慮到導航欄(高度44px)、狀態欄(高度20px)、TabBar(高度49px)會擋住后面scrollView所展示的內容,系統自動為scrollView增加上下的內邊距,這時候我們打印一下tableView的描述(友情提示:tableView繼承自scrollView):
可以看出,contentOffset(內容坐標偏移量)和contentSize(內容尺寸大小)都發生了變化,結合tableView自身的frame可以看出,系統自動為scrollView增加了頂部64px的內邊距以及底部49px的內邊距,正好是導航欄高度+狀態欄高度以及TabBar高度。一旦手動在系統布局頁面之前設置automaticallyAdjustsScrollViewInsets = NO,將會取消上述操作,屆時scrollView內容將會被部分擋住。
PS:上述的情況僅僅對UIScrollView或者子類(如UITableView)有效。
-
contentInsetAdjustmentBehavior
定義及使用(適用于iOS11+,替代automaticallyAdjustsScrollViewInsets)如果只想單純地設置導航條不偏移導航條+狀態欄和Tabbar高度,不想看解釋,可以直接使用該宏定義解決方法適配的問題(宏定義來源:http://www.lxweimin.com/p/352f101d6df1):
// 宏定義解析:看UIScrollView實例是否響應setContentInsetAdjustmentBehavior方法,
響應則賦值2(2表示枚舉值UIScrollViewContentInsetAdjustmentNever),
否則設置視圖控制器的automaticallyAdjustsScrollViewInsets = NO,
_Pragma包括的代碼表示消除-Warc-performSelector-leaks警告。
#define adjustsScrollViewInsets_NO(scrollView,vc)\
do { \
_Pragma("clang diagnostic push") \
_Pragma("clang diagnostic ignored \"-Warc-performSelector-leaks\"") \
if ([UIScrollView instancesRespondToSelector:NSSelectorFromString(@"setContentInsetAdjustmentBehavior:")]) {\
[scrollView performSelector:NSSelectorFromString(@"setContentInsetAdjustmentBehavior:") withObject:@(2)];\
} else {\
vc.automaticallyAdjustsScrollViewInsets = NO;\
}\
_Pragma("clang diagnostic pop") \
} while (0)
首先來看下在Xcode 9的SDK中關于原有屬性automaticallyAdjustsScrollViewInsets的描述:
@property(nonatomic,assign) BOOL automaticallyAdjustsScrollViewInsets
API_DEPRECATED_WITH_REPLACEMENT("Use UIScrollView's contentInsetAdjustmentBehavior instead", ios(7.0,11.0),tvos(7.0,11.0)); // Defaults to YES
// 中文解析:此API已經過期且被UIScrollView下的contentInsetAdjustmentBehavior屬性替代
這里描述得很清楚了,iOS11之后該屬性過期了!那么接下來我們看看替代屬性到底是啥:
/* Configure the behavior of adjustedContentInset.
Default is UIScrollViewContentInsetAdjustmentAutomatic.
中文解析:該屬性用來配置UIScrollView調整內邊距的行為,其值為枚舉值,
默認值是UIScrollViewContentInsetAdjustmentAutomatic,就是自動調整。
*/
@property(nonatomic) UIScrollViewContentInsetAdjustmentBehavior contentInsetAdjustmentBehavior API_AVAILABLE(ios(11.0),tvos(11.0));
// 以下是該枚舉的具體值(選項)
typedef NS_ENUM(NSInteger, UIScrollViewContentInsetAdjustmentBehavior) {
// 中文解析:與UIScrollViewContentInsetAdjustmentScrollableAxes相似,但為了向后兼容(向低版本兼容),
//當scroll view被view controller管理,且該view controller的automaticallyAdjustsScrollViewInsets = YES并在導航條控制器棧內,
//該枚舉也會調整頂部(導航條)和底部(Tabbar)的內邊距,無論該scroll view是否可滾動。
UIScrollViewContentInsetAdjustmentAutomatic,
// Similar to .scrollableAxes, but for backward compatibility will also adjust the top & bottom contentInset
//when the scroll view is owned by a view controller with automaticallyAdjustsScrollViewInsets = YES inside a navigation controller,
//regardless of whether the scroll view is scrollable
// 中文解析:滾動軸的邊緣會被調整(例如contentSize.width/height > frame.size.width/height 或 alwaysBounceHorizontal/Vertical = YES)
UIScrollViewContentInsetAdjustmentScrollableAxes,
// Edges for scrollable axes are adjusted (i.e., contentSize.width/height > frame.size.width/height or alwaysBounceHorizontal/Vertical = YES)
// 中文解析:內邊距不會被調整
UIScrollViewContentInsetAdjustmentNever, // contentInset is not adjusted
// 中文解析:內邊距總是被scroll view的safeAreaInsets所調整,safeAreaInsets顧名思義就是safeArea的內邊距,safeArea下面會有一個概括性的解釋。
UIScrollViewContentInsetAdjustmentAlways,
// contentInset is always adjusted by the scroll view's safeAreaInsets
} API_AVAILABLE(ios(11.0),tvos(11.0));
safeArea概括性解釋:表示一個區域,該區域避開了導航條、Tabbar等可能擋住view controller的view的控件,如果添加一個view控件是相對于它父控件的safeAreaLayoutGuide做布局,則不用擔心被導航條、Tabbar等控件“擋住”
3. 關于iOS7之后與view全屏相關的知識點
在iOS7之后,默認情況下,控制器的view是”全屏“的,也就是說,即便有TabBar或者NavigationBar,控制器的view也是可以”穿透至全屏“的,針對這一特性,蘋果對UIViewController提供了以下幾個屬性供開發者使用:
// 在iOS7之前,控制器的view默認非全屏,如果想要全屏效果,需要設置為YES,該屬性已從iOS7開始過期
@property(nonatomic,assign) BOOL wantsFullScreenLayout NS_DEPRECATED_IOS(3_0, 7_0) __TVOS_PROHIBITED;
// Deprecated in 7_0, Replaced by the following: 該屬性被以下三個屬性代替
// Defaults to UIRectEdgeAll
@property(nonatomic,assign) UIRectEdge edgesForExtendedLayout NS_AVAILABLE_IOS(7_0);
// Defaults to NO, but bars are translucent by default on 7_0\.
@property(nonatomic,assign) BOOL extendedLayoutIncludesOpaqueBars NS_AVAILABLE_IOS(7_0);
// Defaults to YES
@property(nonatomic,assign) BOOL automaticallyAdjustsScrollViewInsets API_DEPRECATED_WITH_REPLACEMENT("Use UIScrollView's contentInsetAdjustmentBehavior instead", ios(7.0,11.0),tvos(7.0,11.0));
下面解釋一下這三個屬性:
edgesForExtendedLayout
:意思是view的邊緣允許額外布局的情況,默認為UIRectEdgeAll,意味著全屏布局(帶穿透效果)。-
extendedLayoutIncludesOpaqueBars
:意思是額外布局是否包括不透明的Bar,默認為NO,意味著如果導航條或者TabBar非透明,view內容不會被他們遮擋,如果該屬性設置為YES,那么在導航條或者TabBar非透明的情況下,view的內容將會被他們遮擋(原點為0,0),該屬性僅僅對非透明的Bar控件有效。示例展示如下圖所示(代碼設置 navigationBar.translucent = NO 并且 extendedLayoutIncludesOpaqueBars = YES)
-
automaticallyAdjustsScrollViewInsets
:意思是是否由系統自動調整滾動視圖的內邊距,默認為YES,意味著系統將會根據導航條和TabBar的情況自動增加上下內邊距以防止滾動視圖的內容被Bar遮擋。
簡單場景快速使用補充:
(如下問題是針對垂直起始布局位置的設置)
- 如果你想讓 UINavigationController-UIViewController 結構下,ViewController 的 View 起始點是從 NavBar 底部開始,添加如下代碼:
self.navigationController.navigationBar.translucent = NO;
此時的 navBar 是不透明且默認為白色;Controller 的 view 起始點從 navBar 底部開始到屏幕底部,如有 tabbar 則會貫穿(注意這里只是設置了navigationBar.translucent,沒有設置 tabbar.translucent,所以此時 view 同樣會貫穿 tabbar的)
- 如果不想修改translucent同時希望具有第1種情況的需求,則可以通過
edgesForExtendedLayout
進行設置,該值默認是UIRectEdgeAll
。
self.edgesForExtendedLayout = UIRectEdgeTop;
此時的 navBar 是默認帶透明樣式的;此時的 view 布局與情況1一樣(如果設置為UIRectEdgeNone則,view 布局是從 navBar 底部到 tabbar 頂部之間)。