iOS开发之适配的相关属性

相关属性标签:edgesForExtendedLayout,safeAreaInsets,translucent,automaticallyAdjustsScrollViewInsets,contentInset,adjustedContentInset,contentInsetAdjustmentBehavior

参考链接:

1.关于edgesForExtendedLayout 与 safeAreaInsets

1.1 edgesForExtendedLayout:iOS7推出,指定边缘要延伸的方向,UIViewController属性
默认UIRectEdgeAll即self.view全屏显示。
1.2 safeAreaInsets:iOS11推出,安全区域,view属性
readonly只读属性,系统控制;同时新增两个方法获取安全区域改变

UIViewController中新增:
- (void)viewSafeAreaInsetsDidChange;
UIView中新增:
- (void)viewSafeAreaInsetsDidChange;

1.3 以下分别测试控制器view的frame,view的safeAreaInsets

iOS12.1下测试,以iPhone X(真机,{375,812})为例

edgesForExtendedLayout view.frame(CGRect) view.safeAreaInsets(UIEdgeInsets)
UIRectEdgeNone (带导航栏) {{0, 88}, {375, 724}} {0, 0, 34, 0}
UIRectEdgeAll(带导航栏) {{0, 0}, {375, 812}} {88, 0, 34, 0}
UIRectEdgeNone (不带导航栏) {{0, 0}, {375, 812}} {44, 0, 34, 0}
UIRectEdgeAll(不带导航栏) {{0, 0}, {375, 812}} {44, 0, 34, 0}

iOS8.1和12.1下测试,以iPhone6(模拟器,{375,667})为例

edgesForExtendedLayout view.frame(CGRect) view.safeAreaInsets(UIEdgeInsets) iOS8.1 view.safeAreaInsets(UIEdgeInsets) iOS12.1
UIRectEdgeNone (带导航栏) {{0, 64}, {375, 603}} 没有该属性 {0, 0, 0, 0}
UIRectEdgeAll(带导航栏) {{0, 0}, {375, 667}} 没有该属性 {64, 0, 0, 0}
UIRectEdgeNone (不带导航栏) {{0, 0}, {375, 667}} 没有该属性 {20, 0, 0, 0}
UIRectEdgeAll(不带导航栏) {{0, 0}, {375, 667}} 没有该属性 {20, 0, 0, 0}

edgesForExtendedLayout值为其他情况时相信你已经知道了,有兴趣请自行测试。

总结:edgesForExtendedLayout属性影响在是否有NavigationBar,Tabbar时view的frame;safeAreaInsets属性影响view的可视范围的frame。

2.translucent:iOS3推出,半透明度,UINavigationBar属性

Default is NO on iOS 6 and earlier. Always YES if barStyle is set to UIBarStyleBlackTranslucent
现在的App基本都支持iOS8.0以上,translucent可以认为默认为YES。translucent为YES,控制器view从坐标(0,0)开始;translucent为NO,控制器view从(0,64)开始。当我们使用滚动视图的时候,比如tableVIew,scrollview时候,建议不要修改translucent属性,就是用系统默认的YES

edgesForExtendedLayout = UIRectEdgeAll 时
iOS12.1下测试,以iPhone X(真机,{375,812})为例

view.frame(CGRect) view.safeAreaInsets(UIEdgeInsets)
translucent = NO (带导航栏) {{0, 88}, {375, 724}} {0, 0, 34, 0}
translucent = YES (带导航栏) {{0, 0}, {375, 812}} {88, 0, 34, 0}
translucent = NO(不带导航栏) {{0, 0}, {375, 812}} {44, 0, 34, 0}
translucent = YES(不带导航栏) {{0, 0}, {375, 812}} {44, 0, 34, 0}

总结:translucent = NO 与 edgesForExtendedLayout = UIRectEdgeNone效果相同。开发中建议translucent = YES使用默认值,在基类中修改edgesForExtendedLayout = UIRectEdgeNone来控制所有控制器View的布局。

3.UIScrollView及其子类

首先,根据以上的默认属性提出一个问题:新建一个工程,让初始的控制器(ViewController)带导航栏,在ViewController的view上添加一个webview,webview的frame设置为self.view.frame。请问webview的内容会不会被导航栏挡住?代码理解如下:
有导航栏,translucent = YES 与 edgesForExtendedLayout = UIRectEdgeAll


- (void)viewDidLoad {
    [super viewDidLoad];

    WKWebView *webview = [[WKWebView alloc] init];
    webview.frame = self.view.frame;
    
    [self.view addSubview:webview];
    
    [webview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"https://www.baidu.com"]]];
}

接下来我们解决了解UIScrollView相关属性
automaticallyAdjustsScrollViewInsets: 默认YES,自动适应滚动视图的内边距,在iOS11系统后,该属性无效。
scrollView.contentInset: 决定滚动视图的内容和边缘距离的属性,iOS11由adjustedContentInset决定
adjustedContentInset: iOS11推出,readonly,When contentInsetAdjustmentBehavior allows, UIScrollView may incorporate its safeAreaInsets into the adjustedContentInset.(当contentInsetAdjustmentBehavior允许时,UIScrollView可以合并它的safeareainset进入adjustedContentInset。)
contentInsetAdjustmentBehavior:内边距适应行为,该值决定adjustedContentInset的值。

官方文档:

typedef NS_ENUM(NSInteger, UIScrollViewContentInsetAdjustmentBehavior) {
    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
    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
    UIScrollViewContentInsetAdjustmentAlways, // contentInset is always adjusted by the scroll view's safeAreaInsets
} API_AVAILABLE(ios(11.0),tvos(11.0));

上述问题的代码在iPhone X,iOS12.1下测试
contentInsetAdjustmentBehavior 以下采用简写:

AdjustmentAutomatic AdjustmentScrollableAxes AdjustmentNever AdjustmentAlways
automaticallyAdjustsScrollViewInsets YES YES YES YES
contentInsetAdjustmentBehavior 3 1 2 3
scrollView.contentInset {{0, 0}, {0, 0}} {{0, 0}, {0, 0}} {{0, 0}, {0, 0}} {{0, 0}, {0, 0}}
self.view.safeAreaInsets {88, 0, 34, 0} {88, 0, 34, 0} {88, 0, 34, 0} {88, 0, 34, 0}
scrollView.adjustedContentInset {88, 0, 34, 0} {88, 0, 34, 0} {0, 0, 0, 0} {88, 0, 34, 0}

测试打印:

po self.automaticallyAdjustsScrollViewInsets
po self.webview.scrollView.contentInsetAdjustmentBehavior
po NSStringFromUIEdgeInsets(self.webview.scrollView.contentInset)
po NSStringFromUIEdgeInsets(self.view.safeAreaInsets)
po NSStringFromUIEdgeInsets(self.webview.scrollView.adjustedContentInset)

总结:UIScrollViewContentInsetAdjustmentAutomatic回去选择一个最适合的适应行为。UIScrollViewContentInsetAdjustmentNever就是adjustedContentInset = contentInsets;
UIScrollViewContentInsetAdjustmentAlways就是adjustedContentInset = safeAreaInsets + contentInsets;
UIScrollViewContentInsetAdjustmentScrollableAxes:它的成立依赖于滚动轴,当垂直方向上的contentSize大于滚动视图的高度时,那么垂直方向上的 insets 就由`safeAreaInsets + contentInsets决定,水平方向上同理。

iOS开发之适配的相关属性

//竖屏状态栏高度
#define Portrait_Status_SafeArea_Height (kDeviceInfo.isiPhoneXSeries ? 44 : 20)
//竖屏底部不带tabbar安全区域高度
#define Portrait_Bottom_SafeArea_Height (kDeviceInfo.isiPhoneXSeries ? 34 : 0)
//竖屏底部带tabbar时安全区域高度
#define Portrait_Tabbar_SafeArea_Height (kDeviceInfo.isiPhoneXSeries ? (49 + 34) : 49)
//横屏底部安全区域的高度
#define Landscal_Bottom_SafeArea_Height (kDeviceInfo.isiPhoneXSeries ? 21 : 0)
//横屏左右安全区域的宽度
#define Landscal_LeftRight_SafeArea_Width (kDeviceInfo.isiPhoneXSeries ? 44 : 49)

现在来回答最初提出的那个问题:我们添加webview时,使用的frame是self.view.frame,automaticallyAdjustsScrollViewInsets为默认YES,在iOS12.1下测试,所以该属性无效,contentInsetAdjustmentBehavior行为未设置,则默认为Automatic即UIScrollViewContentInsetAdjustmentAlways,所以该webview的scrollView.adjustedContentInset为{88, 0, 34, 0},所以内容显示是从导航栏底部开始显示的。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,847评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,208评论 1 292
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,587评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,942评论 0 205
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,332评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,587评论 1 218
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,853评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,568评论 0 198
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,273评论 1 242
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,542评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,033评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,373评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,031评论 3 236
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,073评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,830评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,628评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,537评论 2 269