前言
十年的上饒網站建設經驗,針對設計、前端、開發、售后、文案、推廣等六對一服務,響應快,48小時及時工作處理。成都營銷網站建設的優勢是能夠根據用戶設備顯示端的尺寸不同,自動調整上饒建站的顯示方式,使網站能夠適用不同顯示終端,在瀏覽器中調整網站的寬度,無論在任何一種瀏覽器上瀏覽網站,都能展現優雅布局與設計,從而大程度地提升瀏覽體驗。成都創新互聯從事“上饒網站設計”,“上饒網站推廣”以來,每個客戶項目都認真落實執行。
LayoutGuide這個概念在本人從事iOS開發過程中一直是比較陌生的。直至最近想要了解這個細碎的知識點,就隨手查了一下,發現這個概念從iOS7的top/bottom LayoutGuide,到iOS9 UILayoutGuide類的引入,直至最近的iOS11涉及適配iPhone X,引入了Safe Area概念,并且UIView增加了一個safeAreaLayoutGuide屬性。發現自己真的是知道的太少了,所以決定深入的研究下。
在IOS開發的過程中我們經常會遇到一些緊貼tabbar有工具條之類的頁面,比如說購買、支付等頁面,往往這些頁面有時候在棧底顯示(頁面有tabbar),有時不在(頁面沒有tabbar)。
比如:

這種頁面對于常規的做法是有tabbar的時候設置一套約束,沒有tabbar的時候更新一下約束。但是蘋果提過了一個bottomLayoutGuide可以讓我們更優雅的處理這類問題。
代碼如下:
_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僅需一行我們就可以實現這樣的效果。
同樣來說這種效果對于navigationBar也適用——topLayoutGuide。對應的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];
// 手動觸發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];
// 得到當前視圖的最低基準限制,這個是對于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,這個是視圖最高基準限制
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對創新互聯的支持。
網頁名稱:iOS開發之topLayoutGuide和bottomLayoutGuide的使用小技巧分享
網頁路徑:http://www.js-pz168.com/article4/jipoie.html
成都網站建設公司_創新互聯,為您提供網站收錄、服務器托管、網站內鏈、面包屑導航、關鍵詞優化、網站導航
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯