快速自动化适配iPhone X

peterzeng 6年前
   <p>关于iPhone X的适配,主要需要做的工作点就是针对上下非安全区域的适配。</p>    <p>在iOS开发中,针对于布局存在 xib 与 代码编辑 两种方式。而这两种方式又都支持 绝对布局 与 AutoLayout 两种。</p>    <p>接下来本文将从xib、代码编辑、绝对布局、Autolayout几个布局方式来讲解如何针对iPhone X做自动化适配</p>    <ul>     <li> <p>Xib布局</p> </li>    </ul>    <p>Xib的绝对布局并不灵活,如果想要通过特有因素更改View的Frame则需要通过属性索引来实现。所以这里只针对Xib的AutoLayout来做讲解</p>    <p>首先XCode9的Xib为我们提供了SafeAreaLayout选项,而这个选项并不支持iOS9以前的版本。</p>    <p style="text-align:center"><img src="https://simg.open-open.com/show/59506de36eff4e385a67e93b0d5eaa1e.png"></p>    <p>SafeAreaLayout</p>    <p>那么我们需要针对靠近底部或者顶部非安全区域的View做约束就可以达到理想效果,然而约束的值却不能固定,你还需要兼顾非iPhone X的机型。</p>    <p>那么你可以从以下几点中找到解决方法:</p>    <p>首先我们的布局文件如下:</p>    <ul>     <li> <p>三个相同的Label位于控制器根View的底部</p> </li>    </ul>    <p style="text-align:center"><img src="https://simg.open-open.com/show/b1693479bf14a93a77dee8f3f797b60e.png"></p>    <p>约束文件</p>    <p>首先,如果你是做一个新的页面,那么在设置约束时,添加Constrain to margins属性会帮你大忙,布局文件中的第二个Label就是使用Margin属性进行布局。</p>    <p style="text-align:center"><img src="https://simg.open-open.com/show/b77ddb9a1ca99dc7e8333ec81475e027.png"></p>    <p>Constrain to margins</p>    <p>首先,如果你有一个已经存在的页面,而且已经设置好了约束的布局,那么你可以找到对应的约束属性,勾选它的Relative to margin选项,将此约束属性以指向相对于marigin。</p>    <ul>     <li> <p>双击它 (╯>д<)╯⁽˙³˙⁾</p> </li>    </ul>    <p style="text-align:center"><img src="https://simg.open-open.com/show/bdbb47b3c1f869b8156bd07c16b4e118.jpg"></p>    <p>约束属性 Relative to margin入口</p>    <ul>     <li> <p>勾选它 (╯>д<)╯⁽˙³˙⁾</p> </li>    </ul>    <p style="text-align:center"><img src="https://simg.open-open.com/show/4e999dff6dcdf2722755bafcbfd6660e.png"></p>    <p>Relative to margin</p>    <ul>     <li> <p>接下来我们可以看到这三种布局产生的效果</p> </li>     <li> <p>你可以通过布局视图左下角的View as: (某某机型) ,选择iPhone X机型来快速查看布局应用结果。</p> </li>    </ul>    <p style="text-align:center"><img src="https://simg.open-open.com/show/c0d4b5f45696db8e851cf8104b1b62a5.jpg"></p>    <p>布局效果</p>    <ul>     <li> <p>以及运行的效果</p> </li>    </ul>    <p style="text-align:center"><img src="https://simg.open-open.com/show/1a0ef58107f300271be3ca5d3cf01e91.png"></p>    <p>运行效果</p>    <ul>     <li> <p>可以看到,基础约束并不会对iPhone X底部的非安全区域进行适配,而Constrain to margins 与Relative to margin作用下的约束,则可以完美的适应iPhone X。</p> </li>     <li> <p>使用代码布局</p> </li>    </ul>    <p>代码布局依然可以通AutoLayout进行布局,同时也可以通过分支判断来进行绝对布局。</p>    <ul>     <li> <p>AutoLayout</p> </li>    </ul>    <p>1.如果你需要使用原生的API来进行AutoLayout布局,那么你可以使用NSLayoutAttributeBottomMargin作为Bottom的约束枚举值</p>    <pre>  <code class="language-objectivec">NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem:self.leftLabel >attribute:NSLayoutAttributeBottomMargin relatedBy:NSLayoutRelationEqual toItem:self.rightLabel >attribute:NSLayoutAttributeBottom multiplier:1 constant:0];  [self.view addConstraint:constraint];</code></pre>    <p>2.如果你的Autolayout是通过Masonry进行编辑的,那么你只需要更改底部约束</p>    <ul>     <li> <p>在更早的Masonry版本中</p> </li>    </ul>    <p>由:</p>    <pre>  <code class="language-objectivec">make.bottom.equalTo(self.view.mas_bottom);</code></pre>    <p>改为:</p>    <pre>  <code class="language-objectivec">make.bottom.equalTo(self.view.mas_bottomMargin);</code></pre>    <pre>  <code class="language-objectivec">[self.rightLabel mas_makeConstraints:^(MASConstraintMaker *make) {         make.height.mas_equalTo(100);         make.left.equalTo(self.leftLabel.mas_right);         make.right.equalTo(self.view);         make.bottom.equalTo(self.view.mas_bottomMargin);  }];</code></pre>    <ul>     <li> <p>在新的Masonry版本中,当编译器提醒你找不到mas_bottomMargin时,通过:</p> </li>    </ul>    <p>由:</p>    <pre>  <code class="language-objectivec">make.bottom.equalTo(self.view.mas_bottom);</code></pre>    <p>改为:</p>    <pre>  <code class="language-objectivec">make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom);</code></pre>    <pre>  <code class="language-objectivec">[self.rightLabel mas_makeConstraints:^(MASConstraintMaker *make) {         make.height.mas_equalTo(100);         make.left.equalTo(self.leftLabel.mas_right);         make.right.equalTo(self.view);         if (@available(iOS 11.0, *)) {             make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom);         } else {             make.bottom.equalTo(self.view.mas_bottom);         }  }];</code></pre>    <ul>     <li> <p>完整代码如下</p> </li>    </ul>    <pre>  <code class="language-objectivec">- (void)viewDidLoad {      [super viewDidLoad];            self.title = @"代码布局";      [self initSubViews];      [self initLayout];  }  - (void)initSubViews{      UILabel *leftLabel = [[UILabel alloc] init];      leftLabel.text = @"基础约束";      leftLabel.numberOfLines = 0;      leftLabel.textAlignment = NSTextAlignmentCenter;      leftLabel.backgroundColor = [UIColor orangeColor];            UILabel *rightLabel = [[UILabel alloc] init];      rightLabel.text = @"Constrain to margins";      rightLabel.numberOfLines = 0;      rightLabel.textAlignment = NSTextAlignmentCenter;      rightLabel.backgroundColor = [UIColor blueColor];            [self.view addSubview:leftLabel];      [self.view addSubview:rightLabel];            self.leftLabel = leftLabel;      self.rightLabel = rightLabel;  }  - (void)initLayout{      [self.leftLabel mas_makeConstraints:^(MASConstraintMaker *make) {          make.height.mas_equalTo(100);          make.width.equalTo(self.view).multipliedBy(0.5);          make.left.equalTo(self.view);          make.bottom.equalTo(self.view.mas_bottom);      }];            [self.rightLabel mas_makeConstraints:^(MASConstraintMaker *make) {          make.height.mas_equalTo(100);          make.left.equalTo(self.leftLabel.mas_right);          make.right.equalTo(self.view);          if (@available(iOS 11.0, *)) {              make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom);          } else {              make.bottom.equalTo(self.view.mas_bottom);          }      }];  }</code></pre>    <ul>     <li> <p>以及运行的效果</p> </li>    </ul>    <p style="text-align:center"><img src="https://simg.open-open.com/show/c66c590b4a0f4d6dddf25b94ddbcfa8b.png"></p>    <p>代码布局</p>    <ul>     <li> <p>使用代码布局</p> </li>    </ul>    <ul>     <li> <p>绝对布局</p> </li>    </ul>    <p>如果你需要使用代码进行绝对布局,那么iOS11中View的safeAreaInsets属性可以帮到你。因为safeAreaInsets最低支持iOS11的缘故,所以你需要加入版本判断来使用。safeAreaInsets会给出你上下左右各个方位的非安全区域的大小,你可以通过这些值来设置自己的View的位置。</p>    <pre>  <code class="language-objectivec">@property (nonatomic,readonly) UIEdgeInsets safeAreaInsets API_AVAILABLE(ios(11.0),tvos(11.0));</code></pre>    <ul>     <li> <p>在这里我准备了几个宏供大家使用</p> </li>    </ul>    <pre>  <code class="language-objectivec">#define IOS11_OR_LATER_SPACE(par)   ({  float space = 0.0;  if (@available(iOS 11.0, *))  space = par;  (space);  })  #define JF_KEY_WINDOW [UIApplication sharedApplication].keyWindow  #define JF_TOP_SPACE IOS11_OR_LATER_SPACE(JF_KEY_WINDOW.safeAreaInsets.top)  #define JF_TOP_ACTIVE_SPACE IOS11_OR_LATER_SPACE(MAX(0, JF_KEY_WINDOW.safeAreaInsets.top-20))  #define JF_BOTTOM_SPACE IOS11_OR_LATER_SPACE(JF_KEY_WINDOW.safeAreaInsets.bottom)</code></pre>    <ul>     <li> <p>宏里已经进行了版本判断,如果你需要设置一个View置于控制器根View的底部,那么只需要通过</p> </li>    </ul>    <pre>  <code class="language-objectivec">- (void)createView{     UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];     view.backgroundColor = [UIColor redColor];     [self.view addSubview:view];          self.bottomView = view;  }  - (void)viewDidLayoutSubviews{     [super viewDidLayoutSubviews];          CGRect frame = self.bottomView.frame;     frame.origin.y = self.view.bounds.size.height - frame.size.height - JF_BOTTOM_SPACE;     self.bottomView.frame = frame;  }</code></pre>    <p>以上代码,来减去非安全区的位置即可</p>    <p>PS:safeAreaInsets还可以用来进行设置连接于View边缘的ScrollView的Insets额外滑动区域</p>    <p>Demo下载: <a href="/misc/goto?guid=4959755233361112129" rel="nofollow,noindex">https://github.com/Jiang-Fallen/LayoutiPhoneX</a></p>    <p>联系邮箱:Jiang.Fallen@gmail.com</p>    <p> </p>    <p>来自: <a href="/misc/goto?guid=4959755233451407125" rel="nofollow,noindex">http://www.jianshu.com/p/263e08f1ad6e</a></p>    <p> </p>