iOS開發(fā)之topLayoutGuide和bottomLayoutGuide的使用小技巧分享
前言
LayoutGuide這個(gè)概念在本人從事iOS開發(fā)過程中一直是比較陌生的。直至最近想要了解這個(gè)細(xì)碎的知識點(diǎn),就隨手查了一下,發(fā)現(xiàn)這個(gè)概念從iOS7的top/bottom LayoutGuide,到iOS9 UILayoutGuide類的引入,直至最近的iOS11涉及適配iPhone X,引入了Safe Area概念,并且UIView增加了一個(gè)safeAreaLayoutGuide屬性。發(fā)現(xiàn)自己真的是知道的太少了,所以決定深入的研究下。
在IOS開發(fā)的過程中我們經(jīng)常會遇到一些緊貼tabbar有工具條之類的頁面,比如說購買、支付等頁面,往往這些頁面有時(shí)候在棧底顯示(頁面有tabbar),有時(shí)不在(頁面沒有tabbar)。
比如:

這種頁面對于常規(guī)的做法是有tabbar的時(shí)候設(shè)置一套約束,沒有tabbar的時(shí)候更新一下約束。但是蘋果提過了一個(gè)bottomLayoutGuide可以讓我們更優(yōu)雅的處理這類問題。
代碼如下:
_bottomView = [UIView new];
_bottomView.backgroundColor = [UIColor yellowColor];
[self.view addSubview:_bottomView];
[_bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
make.height.equalTo(@40);
make.left.and.right.equalTo(self.view);
make.bottom.equalTo(self.mas_bottomLayoutGuide);
}];
搭配Masonry,使用Masonry提供的mas_bottomLayoutGuide僅需一行我們就可以實(shí)現(xiàn)這樣的效果。
同樣來說這種效果對于navigationBar也適用——topLayoutGuide。對應(yīng)的Masonry使用方法是mas_topLayoutGuide。
完整代碼(代碼量太少就不給完整的鏈接了):
#import "ViewController.h"
#import "Masonry.h"
@interface ViewController ()
@property (strong, nonatomic) UIView *topView;
@property (strong, nonatomic) UIView *bottomView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(0, 164, 80, 50);
[btn setTitle:@"top" forState:UIControlStateNormal];
btn.backgroundColor = [UIColor redColor];
[btn addTarget:self action:@selector(topClick) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
btn1.backgroundColor = [UIColor yellowColor];
btn1.frame = CGRectMake(0, 264, 80, 50);
[btn1 setTitle:@"bottom" forState:UIControlStateNormal];
[btn1 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[btn1 addTarget:self action:@selector(bottomClick) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn1];
[self initView];
}
- (void)initView {
_topView = [UIView new];
_topView.backgroundColor = [UIColor greenColor];
[self.view addSubview:_topView];
[_topView mas_makeConstraints:^(MASConstraintMaker *make) {
make.height.equalTo(@40);
make.left.and.right.equalTo(self.view);
make.top.equalTo(self.mas_topLayoutGuide);
}];
_bottomView = [UIView new];
_bottomView.backgroundColor = [UIColor yellowColor];
[self.view addSubview:_bottomView];
[_bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
make.height.equalTo(@40);
make.left.and.right.equalTo(self.view);
make.bottom.equalTo(self.mas_bottomLayoutGuide);
}];
}
- (void)topClick{
[self.navigationController setNavigationBarHidden:!self.navigationController.navigationBarHidden animated:NO];
// [self updateViewConstraints];
}
- (void)bottomClick{
[self.navigationController setToolbarHidden:!self.navigationController.toolbarHidden animated:NO];
// 手動(dòng)觸發(fā)updateViewConstraints
// [self updateViewConstraints];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
附:iOS 使用LayoutGuide 來限制控件的位置,配合Auto Layout constraints
UIButton *button = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[self.view addSubview:button];
[button setTranslatesAutoresizingMaskIntoConstraints: NO];
// 得到當(dāng)前視圖的最低基準(zhǔn)限制,這個(gè)是對于Auto Layout constraint來說的。
id bottomGuide = self.bottomLayoutGuide;
NSDictionary *viewsDictionary = NSDictionaryOfVariableBindings (button, bottomGuide);
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat: @"V:[button]-20-[bottomGuide]"
options: 0
metrics: nil
views: viewsDictionary]];
[self.view layoutSubviews];
同理可以得到topLayoutGuide,這個(gè)是視圖最高基準(zhǔn)限制
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。
相關(guān)文章
iOS實(shí)現(xiàn)翻頁效果動(dòng)畫實(shí)例代碼
本篇文章主要介紹了iOS實(shí)現(xiàn)翻頁效果動(dòng)畫實(shí)例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-05-05
iOS tableview實(shí)現(xiàn)簡單搜索功能
這篇文章主要為大家詳細(xì)介紹了iOS tableview實(shí)現(xiàn)簡單搜索功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-11-11
詳解iOS如何讓Lottie使用網(wǎng)絡(luò)資源做動(dòng)畫的實(shí)現(xiàn)
這篇文章主要為大家介紹了iOS如何讓Lottie使用網(wǎng)絡(luò)資源做動(dòng)畫實(shí)現(xiàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02
ios 實(shí)現(xiàn)倒計(jì)時(shí)的兩種方式
這篇文章主要介紹了ios實(shí)現(xiàn)倒計(jì)時(shí)的兩種方式,第一種方式使用NSTimer來實(shí)現(xiàn),第二種方式使用GCD來實(shí)現(xiàn)。具體內(nèi)容詳情大家參考下本文2017-01-01
iOS 11 使用兩種方法替換(Method Swizzling)去掉導(dǎo)航欄返回按鈕的文字
這篇文章主要介紹了iOS 11 使用方法替換(Method Swizzling)去掉導(dǎo)航欄返回按鈕的文字,需要的朋友可以參考下2018-05-05

