translucent
translucent屬性能決定UITabBar/UINavigationBar是否為半透明的效果.這個BOOL屬性能控制UITabBar/UINavigationBar的半透明效果,默認為YES,即默認情況下為半透明效果.
默認情況下,如果使用UITabBarController和UINavigationBarController(translucent屬性默認為YES)必指。
設置一個UITableView括饶,距離邊距是0.
UITableView *tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds];
tableView.backgroundColor = [UIColor redColor];
tableView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
我們看到tableView雖然是占住整個屏幕但是還是沒有遮擋住cell,這是因為系統(tǒng)默認默認控制器屬性automaticallyAdjustsScrollViewInsets默認為YES来涨。
請注意:iOS11開始图焰,蘋果摒棄了automaticallyAdjustsScrollViewInsets屬性,改由contentInsetAdjustmentBehavior(枚舉值)控制蹦掐。
automaticallyAdjustsScrollViewInsets = YES時系統(tǒng)底層所干的事:
scrollView的內(nèi)容原本沒有內(nèi)邊距技羔,但是考慮到導航欄(高度44px)、狀態(tài)欄(高度20px)卧抗、TabBar(高度49px)會擋住后面scrollView所展示的內(nèi)容藤滥,系統(tǒng)自動為scrollView增加上下的內(nèi)邊距。
一旦手動在系統(tǒng)布局頁面之前設置automaticallyAdjustsScrollViewInsets = NO社裆,將會取消上述操作拙绊,屆時scrollView內(nèi)容將會被部分擋住。
請注意:上述的情況僅僅對UIScrollView或者子類(如UITableView)有效泳秀。
當我們添加:
self.navigationController.navigationBar.translucent = NO;
此時tableView的往下移動了64px标沪。
總結(jié):
1、navigationBar.translucent 默認是YES嗜傅,此時布局的起始點是(0,0)金句。
2、navigationBar.translucent 設置為NO吕嘀,原點坐標在(0,64)违寞。
當navigationBar.translucent為YES,automaticallyAdjustsScrollViewInsets 設置為No的時候偶房,此時cell被遮擋住.
當navigationBar.translucent為NO趁曼,automaticallyAdjustsScrollViewInsets 設置為No的時候,此時cell上面沒被遮擋住.
當navigationBar.translucent = NO棕洋, tabBar.translucent = NO 的時候彰阴,下面其實還是被遮擋住。
automaticallyAdjustsScrollViewInsets用法
1拍冠、單獨設置self.automaticallyAdjustsScrollViewInsets
self.automaticallyAdjustsScrollViewInsets = NO;
2尿这、self.edgesForExtendedLayout聯(lián)合設置,原點就是(0庆杜,64)開始
self.automaticallyAdjustsScrollViewInsets = NO;
self.edgesForExtendedLayout = UIRectEdgeNone;
當屏幕會多出一個64的高度的時候射众,系統(tǒng)就會自動根據(jù)UINavigationBar和statusBar將view下移64,frame從(0晃财,64)開始叨橱。這樣典蜕,我們在布局內(nèi)部控件的時候依然可以從(0,0)開始罗洗,而不必擔心上部被UINavigationBar遮擋了.
iOS11+,contentInsetAdjustmentBehavior定義及使用(替代automaticallyAdjustsScrollViewInsets)
如果只想單純地設置導航條不偏移導航條+狀態(tài)欄和Tabbar高度
/* Configure the behavior of adjustedContentInset.
Default is UIScrollViewContentInsetAdjustmentAutomatic.
中文解析:該屬性用來配置UIScrollView調(diào)整內(nèi)邊距的行為愉舔,其值為枚舉值,默認值是UIScrollViewContentInsetAdjustmentAutomatic伙菜,就是自動調(diào)整轩缤。
*/
@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并在導航條控制器棧內(nèi),該枚舉也會調(diào)整頂部(導航條)和底部(Tabbar)的內(nèi)邊距淑倾,無論該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
// 中文解析:滾動軸的邊緣會被調(diào)整(例如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)
// 中文解析:內(nèi)邊距不會被調(diào)整
UIScrollViewContentInsetAdjustmentNever, // contentInset is not adjusted
// 中文解析:內(nèi)邊距總是被scroll view的safeAreaInsets所調(diào)整,safeAreaInsets顧名思義就是safeArea的內(nèi)邊距娇哆,safeArea下面會有一個概括性的解釋湃累。
UIScrollViewContentInsetAdjustmentAlways, // contentInset is always adjusted by the scroll view's safeAreaInsets
} API_AVAILABLE(ios(11.0),tvos(11.0));
非ScrollView的布局需求
UINavigationBar和statusBar 在默認情況下都是半透明的,要求:在上面添加控件碍讨,從(0脱茉,0)點開始布局,內(nèi)容不被遮擋垄开,可以正常顯示琴许。
UINavigationBar和statusBar保留半透明效果時
1、手動布局溉躲,計算UINavigationBar和statusBar的占位榜田,上面從(0,64)開始布局锻梳,整個空間的布局長度是[UIScreen mainScreen].bounds.size.heigh-64-49,iphoneX的時候則是:[UIScreen mainScreen].bounds.size.heigh-88-49-34箭券。
image.png
2、修改viewController的edgesForExtendedLayout屬性疑枯,edgesForExtendedLayout = UIRectEdgeNone辩块。設置后,控制器的view的frame的坐標Y增加64px緊挨著navigationBar下方荆永,底部同理废亭,該屬性支持iOS7及以后的版本。(注意:這里雖然看著導航條和TabBar還有半透明效果具钥,但是實際上下面的內(nèi)容已經(jīng)無法再”穿透“了豆村。)
UINavigationBar和statusBar不保留半透明效果時
UINavigationBar和statusBar設置為NO,此時NavigationBar和statusBar都是白色的不透明
iOS11:
在iOS11中UIViewController的automaticallyAdjustsScrollViewInsets屬性被廢棄骂删,不再起作用掌动, 取而代之的是UIScrollView中新增的屬性contentInsetAdjustmentBehavior四啰。
self.extendedLayoutIncludesOpaqueBars = YES;
if (@available(iOS 11.0, *)) {
self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
} else {
self.automaticallyAdjustsScrollViewInsets = NO;
}
_tableView.contentInset = UIEdgeInsetsMake(64, 0, 49, 0);
_tableView.scrollIndicatorInsets = _tableView.contentInset;
全屏效果的app
在ios7之后,為了達到全屏效果粗恢,在UIViewController中添加了幾個屬性:
@property(nonatomic,assign) UIRectEdge edgesForExtendedLayout NS_AVAILABLE_IOS(7_0); // Defaults to UIRectEdgeAll
@property(nonatomic,assign) BOOL extendedLayoutIncludesOpaqueBars NS_AVAILABLE_IOS(7_0); // Defaults to NO, but bars are translucent by default on 7_0.
@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
edgesForExtendedLayout:離四周的距離柑晒,默認UIRectEdgeAll,意為上下左右填充滿整個屏幕眷射。
UIViewController添加到UINavigationController上時匙赞,默認UIViewController的原點是在(0,0)處凭迹。
當self.edgesForExtendedLayout = UIRectEdgeNone;時,
UIViewController的原點是在(0苦囱,64/88)處嗅绸。
UIViewController.y = UINavigationBar.y + UINavigationBar.h;
此時可以設置navigationBar.translucent = NO撕彤,讓導航欄不半透明鱼鸠。
UITableViewController添加到UITabBarController上時,UITableViewController的底部一部分cell會被TabBar擋住.
設置:self.edgesForExtendedLayout = UIRectEdgeNone;
UITableViewController.tabbar的y = CGRectGetMaxY(UITableViewController);
extendedLayoutIncludesOpaqueBars:延伸視圖包含不包含不透明的Bar,是用來指定導航欄是透明的還是不透明.
YES 是透明羹铅,NO是不透明蚀狰。
automaticallyAdjustsScrollViewInsets
YES時,scrollView职员、tableview麻蹋,在設置完數(shù)據(jù)的時候,內(nèi)部會改變contentInsets的top值為64焊切。