總所周知,蘋果從iOS7開始采用扁平化的界面風格,顛覆了果粉們“迷戀”的擬物化風格。對于開發者而言,全新的風格帶來新的接口,這些新的接口改動中,有些更加合理了,有些更加方便了,而有些可能讓開發者容易迷糊,下面本人就來談談iOS7這些新添加“鬼魅”的接口中的經常接觸到的一個----UITabBar/UINavigationBar的translucent屬性。
新的屬性translucent簡介
顧名思義,translucent屬性能決定UITabBar/UINavigationBar是否為半透明的效果,蘋果官方對此的解釋如下:
- UINavigationBar的translucent屬性解釋
/*
New behavior on iOS 7.
Default is YES.
You may force an opaque background by setting the property to NO.
If the navigation bar has a custom background image, the default is inferred
from the alpha values of the image—YES if it has any pixel with alpha < 1.0
If you send setTranslucent:YES to a bar with an opaque custom background image
it will apply a system opacity less than 1.0 to the image.
If you send setTranslucent:NO to a bar with a translucent custom background image
it will provide an opaque background for the image using the bar's barTintColor if defined, or black
for UIBarStyleBlack or white for UIBarStyleDefault if barTintColor is nil.
*/
@property(nonatomic,assign,getter=isTranslucent) BOOL translucent NS_AVAILABLE_IOS(3_0) UI_APPEARANCE_SELECTOR;
// Default is NO on iOS 6 and earlier. Always YES if barStyle is set to UIBarStyleBlackTranslucent
- UITabBar的translucent屬性解釋
/*
Default is YES.
You may force an opaque background by setting the property to NO.
If the tab bar has a custom background image, the default is inferred from the alpha
values of the image—YES if it has any pixel with alpha < 1.0
If you send setTranslucent:YES to a tab bar with an opaque custom background image
the tab bar will apply a system opacity less than 1.0 to the image.
If you send setTranslucent:NO to a tab bar with a translucent custom background image
the tab bar will provide an opaque background for the image using the bar's barTintColor if defined, or black
for UIBarStyleBlack or white for UIBarStyleDefault if barTintColor is nil.
*/
@property(nonatomic,getter=isTranslucent) BOOL translucent NS_AVAILABLE_IOS(7_0);
簡而言之,這個BOOL屬性能控制UITabBar/UINavigationBar的半透明效果,默認為YES,即默認情況下為半透明效果(后面會詳細解釋這兩段官方文字描述的含義)。
translucent緣何“鬼魅”
新屬性的出現往往會導致一些“注意事項”,主要體現為被半透明效果處理后產生的色差和添加的類view控件的坐標變動。
默認情況下,如果使用UITabBarController和UINavigationBarController(translucent屬性默認為YES),設置一個藍色的view添加其中并設置距離屏幕邊距為(0,0,0,0),展示效果如下:
可以看到,UITabBarController和UINavigationBarController被藍色的view“穿透”了,此時view的邊距也正如設置的一樣,零點坐標在(0,0)處。
這時候,如果將view替換成tableView,展示效果如下:
可以看到,tableView的cell并沒有因為“穿透”效果而出現被遮擋的情況,這是由于蘋果對滾動視圖的特殊性進行處理:對于類ScrollView,系統默認控制器屬性automaticallyAdjustsScrollViewInsets默認為YES。
請注意:iOS11開始,蘋果摒棄了automaticallyAdjustsScrollViewInsets屬性,改由contentInsetAdjustmentBehavior(枚舉值)控制,下面會有詳細的解釋。
-
automaticallyAdjustsScrollViewInsets = YES時系統底層所干的事
scrollView的內容原本沒有內邊距,但是考慮到導航欄(高度44px)、狀態欄(高度20px)、TabBar(高度49px)會擋住后面scrollView所展示的內容,系統自動為scrollView增加上下的內邊距,這時候我們打印一下tableView的描述(友情提示:tableView繼承自scrollView):
tableView描述.png
可以看出,contentOffset(內容坐標偏移量)和contentSize(內容尺寸大小)都發生了變化,結合tableView自身的frame可以看出,系統自動為scrollView增加了頂部64px的內邊距以及底部49px的內邊距,正好是導航欄高度+狀態欄高度以及TabBar高度。一旦手動在系統布局頁面之前設置automaticallyAdjustsScrollViewInsets = NO,將會取消上述操作,屆時scrollView內容將會被部分擋住。
請注意:上述的情況僅僅對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等控件“擋住”
-
非滾動視圖中導致“鬼魅”的原因
從上述內容中,我們可以了解到滾動視圖默認情況下系統會給滾動內容增加上下內邊距,以防內容被導航條和TabBar遮擋,但是實際上在常用的項目界面結構(TabBarController--NavigationController--ViewController)以及系統默認情況下,viewController中的view是沒有內邊距的(也就是說view穿透上下兩個Bar)。但是我們有時候會看到應用中會有這樣的情形:非滾動視圖依然有類似增加上下內邊距的效果,自己并沒有手動更改過視圖的frame。這種情況,就要結合其他幾個系統的屬性來解釋了。
針對非滾動視圖的需求
由上述內容我們知道,默認情況下,導航條和TabBar都是半透明,添加在上面的控制器的視圖會有“穿透”效果。如果現在有這樣的需求:對于非滾動視圖,從原點(0,0)布局,但是內容不被遮擋,能夠正常顯示。
這時候我們需要區分兩種情況:是否需要導航條/TabBar帶有半透明效果。
-
保留半透明效果(導航條/TabBar的translucent == YES)
- 手動修改frame布局,從(0,64)開始布局,底部同理,需要計算坐標尺寸,比較繁瑣。
-
修改viewController的edgesForExtendedLayout屬性,edgesForExtendedLayout = UIRectEdgeNone
(備注:設置后,控制器的view的frame的坐標Y增加64px緊挨著navigationBar下方,底部同理,該屬性支持iOS7及以后的版本。)
注意:這里雖然看著導航條和TabBar還有半透明效果,但是實際上下面的內容已經無法再”穿透“了。
運行效果如下圖所示:
edgesForExtendedLayout = UIRectEdgeNone效果展示.png
-
不保留半透明效果(導航條/TabBar的translucent == NO)
將TabBar和導航條的translucent屬性分別設置為NO,不做任何其他修改,此時bar的背景顏色為默認的白色,且不再有色差,運行效果如下圖所示:
導航條的translucent設置為NO效果展示.png
TabBar的translucent設置為NO效果展示.png
關于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 NS_AVAILABLE_IOS(7_0);
下面解釋一下這三個屬性:
edgesForExtendedLayout:意思是view的邊緣允許額外布局的情況,默認為UIRectEdgeAll,意味著全屏布局(帶穿透效果)。
-
extendedLayoutIncludesOpaqueBars:意思是額外布局是否包括不透明的Bar,默認為NO,意味著如果導航條或者TabBar非透明,view內容不會被他們遮擋,如果該屬性設置為YES,那么在導航條或者TabBar非透明的情況下,view的內容將會被他們遮擋(原點為0,0),該屬性僅僅對非透明的Bar控件有效。
示例展示如下圖所示(代碼設置 navigationBar.translucent = NO 并且 extendedLayoutIncludesOpaqueBars = YES)
非透明導航條遮擋內容效果展示.png automaticallyAdjustsScrollViewInsets:意思是是否由系統自動調整滾動視圖的內邊距,默認為YES,意味著系統將會根據導航條和TabBar的情況自動增加上下內邊距以防止滾動視圖的內容被Bar遮擋。
設置導航條或者TabBar背景圖片的注意事項
這里會詳細解釋官方文檔對translucent屬性的注釋,為什么放到這里才說?因為官方文檔對該屬性的解釋全部跟設置導航條或者TabBar的背景圖片有關!說明蘋果也知道,這里坑很多,下面就來梳理一下吧。。。
translucent屬性的官方解釋
- UINavigationBar/UITabBar的translucent屬性解釋:默認為YES,可以通過設置NO來強制使用非透明背景,如果導航條使用自定義背景圖片,那么默認情況該屬性的值由圖片的alpha(透明度)決定,如果alpha的透明度小于1.0值為YES。如果手動設置translucent為YES并且使用自定義不透明圖片,那么會自動設置系統透明度(小于1.0)在這個圖片上。如果手動設置translucent為NO并且使用自定義帶透明度(透明度小于0)的圖片,那么系統會展示這張背景圖片,只不過這張圖片會使用事先確定的barTintColor進行不透明處理,若barTintColor為空,則會使用UIBarStyleBlack(黑色)或者UIBarStyleDefault(白色)。
設置導航欄背景圖片透明度問題
- 如果背景圖片沒有透明度,系統會自動把導航控制器的棧頂控制器的view的Y值增加64,如果沒有透明度,則不會增加。
- 這一情況的圖片必須是imageSet格式的,并且圖片的透明度為1(不透明),系統才會去調整。