一稠腊、UIViewController的automaticallyAdjustsScrollViewInsets屬性
@property(nonatomic,assign) BOOL automaticallyAdjustsScrollViewInsets;// Defaults to YES
API_DEPRECATED("Use UIScrollView's contentInsetAdjustmentBehavior instead", ios(7.0,11.0),tvos(7.0,11.0))
UIViewController的一個(gè)屬性:(iOS7.0引入锅风,11.0廢除扯夭,之后其作用被UIScrollView的新屬性contentInsetAdjustmentBehavior所取代竭讳,如設(shè)置為UIScrollViewContentInsetAdjustmentAutomatic等);
作用:默認(rèn)情況下,它可以保證滾動視圖的內(nèi)容自動偏移材失,不會被UINavigationBar與UITabBar遮擋磨淌。
簡要說明:
UINavigationBar與UITabBar默認(rèn)都是半透明模糊效果,在這種情況下系統(tǒng)會對視圖控制器的UI布局進(jìn)行優(yōu)化:當(dāng)視圖控制器里面【第一個(gè)】被添加進(jìn)去的視圖是UIScrollView或其子類時(shí)望拖,系統(tǒng)會自動調(diào)整其內(nèi)邊距屬性contentInset渺尘,以保證滑動視圖里的內(nèi)容不被UINavigationBar與UITabBar遮擋。
(一)说敏、automaticallyAdjustsScrollViewInsets為YES時(shí)(即默認(rèn)情況):
(A).UINavigationBar與UITabBar的translucent屬性為YES(也即默認(rèn)情況下)
//UITextView是滑動視圖沧烈,內(nèi)容自動向下偏移,不會被導(dǎo)航欄覆蓋
//leftTextView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentAutomatic;
UITextView *leftTextView = [[UITextView alloc] init];
leftTextView.frame = CGRectMake(0, 0,100, 736);
leftTextView.backgroundColor = [UIColor lightGrayColor];
leftTextView.text = @"君不見像云,黃河之水天上來,奔流到海不復(fù)回蚂夕。君不見迅诬,高堂明鏡悲白發(fā),朝如青絲暮成雪婿牍。人生得意須盡歡侈贷,莫使金樽空對月。天生我材必有用等脂,千金散盡還復(fù)來俏蛮。";
leftTextView.font = [UIFont systemFontOfSize:18];
leftTextView.editable = NO;
[self.view addSubview:leftTextView];
//UIView是非滑動視圖,內(nèi)容被導(dǎo)航欄部分覆蓋
UIView *rightView= [[UIView alloc] initWithFrame:CGRectMake(150, 0, 100, 100)];
rightView.backgroundColor = [UIColor redColor];
[self.view addSubview:rightView];
結(jié)論:UITextView未被遮擋上遥,rightView被遮擋搏屑,這是最常見的情況。
(B).UINavigationBar與UITabBar的translucent屬性為NO時(shí):
結(jié)論:UITextView未被遮擋粉楚,rightView也未被遮擋辣恋。
總結(jié):默認(rèn)情況下亮垫,滾動視圖的內(nèi)容不會被UINavigationBar與UITabBar所遮擋,其contentInset會自動調(diào)整伟骨;非滾動視圖會不會被遮擋饮潦,取決于UINavigationBar與UITabBar是否半透明。
(二)携狭、設(shè)置automaticallyAdjustsScrollViewInsets為NO時(shí):
(A).UINavigationBar與UITabBar的translucent屬性為YES(也即默認(rèn)情況下)
結(jié)論:UITextView被遮擋继蜡,rightView也被遮擋。
(B).UINavigationBar與UITabBar的translucent屬性為NO時(shí):
結(jié)論:UITextView未被遮擋逛腿,rightView也未遮擋稀并。
總結(jié):設(shè)置automaticallyAdjustsScrollViewInsets為NO時(shí),滾動視圖被視為普通的view鳄逾,其內(nèi)容會不會被UINavigationBar與UITabBar所遮擋取決于UINavigationBar與UITabBar是否半透明:半透明:會被遮擋稻轨;不透明,不會被遮擋雕凹。(額殴俱。。枚抵。貌似UITableView例外线欲,如論如何,其內(nèi)容都不會遮擋汽摹。李丰。。)
綜上所述
automaticallyAdjustsScrollViewInsets的設(shè)置只對滾動視圖有效逼泣,對普通的view無效趴泌;對普通view而言,UINavigationBar與UITabBar半透明:會被遮擋拉庶;不透明嗜憔,不會被遮擋。如果兩個(gè)都是默認(rèn)情況下氏仗,則滾動視圖的內(nèi)容不會被遮擋吉捶,普通的view會被遮擋,這是最常見的情況皆尔。
二呐舔、UIViewController的edgesForExtendedLayout屬性
typedef NS_OPTIONS(NSUInteger, UIRectEdge) {
UIRectEdgeNone = 0,
UIRectEdgeTop = 1 << 0,
UIRectEdgeLeft = 1 << 1,
UIRectEdgeBottom = 1 << 2,
UIRectEdgeRight = 1 << 3,
UIRectEdgeAll = UIRectEdgeTop | UIRectEdgeLeft | UIRectEdgeBottom | UIRectEdgeRight
} NS_ENUM_AVAILABLE_IOS(7_0);
@property(nonatomic,assign) UIRectEdge edgesForExtendedLayout NS_AVAILABLE_IOS(7_0); // Defaults to UIRectEdgeAll
edgesForExtendedLayout:邊緣延伸屬性,默認(rèn)為UIRectEdgeAll慷蠕。它也是視圖控制器的布局屬性珊拼,默認(rèn)值是UIRectEdgeAll,即:當(dāng)前視圖控制器里各種UI控件【本身】(而非內(nèi)容)會忽略導(dǎo)航欄和標(biāo)簽的存在流炕,布局時(shí)若設(shè)置其原點(diǎn)設(shè)置為(0,0)杆麸,視圖會延伸顯示到導(dǎo)航欄的下面被覆蓋搁进;其值為UIRectEdgeNone意味著子控件本身會自動躲避導(dǎo)航欄和標(biāo)簽欄,以免被遮擋昔头。
self.edgesForExtendedLayout = UIRectEdgeNone;
三饼问、導(dǎo)航欄的translucent屬性
@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
默認(rèn)為YES,意味著子控件以(0,0)點(diǎn)為坐標(biāo)系布局揭斧;
設(shè)置為NO時(shí)莱革,意味著子控件以(0,64)點(diǎn)為坐標(biāo)系布局(不太準(zhǔn)確,應(yīng)為導(dǎo)航欄左下角為坐標(biāo)系原點(diǎn))讹开;
self.navigationController.navigationBar.translucent = NO;