在IOS開發(fā)的過程中我們經(jīng)常會遇到一些緊貼tabbar有工具條之類的頁面河泳,比如說購買沃呢、支付等頁面,往往這些頁面有時候在棧底顯示(頁面有tabbar)拆挥,有時不在(頁面沒有tabbar)薄霜。比如:
這種頁面對于常規(guī)的做法是有tabbar的時候設(shè)置一套約束,沒有tabbar的時候更新一下約束。但是蘋果提過了一個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僅需一行我們就可以實現(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];
// 手動觸發(fā)updateViewConstraints
// [self updateViewConstraints];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end