利用Masonry自適應鍵盤高度
阿新 • • 發佈:2019-02-14
正文:
前期準備:
1. 下載Masonry並匯入到工程中;
2. 將Masonry.h匯入當前控制器。
案例一:
要求:
無論在什麼尺寸的裝置上(包括橫豎屏切換),紅色view都居中顯示。
案例一
實現:
#import "ViewController.h"
#import "Masonry.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// 防止block中的迴圈引用
__weak typeof(self) weakSelf = self ;
// 初始化view並設定背景
UIView *view = [UIView new];
view.backgroundColor = [UIColor redColor];
[self.view addSubview:view];
// 使用mas_makeConstraints新增約束
[view mas_makeConstraints:^(MASConstraintMaker *make) {
// 新增大小約束(make就是要新增約束的控制元件view)
make.size.mas_equalTo(CGSizeMake(100, 100));
// 新增居中約束(居中方式與self相同)
make.center.equalTo(weakSelf.view); }];
}
@end
案例二:
要求:
- 無論在什麼尺寸的裝置上(包括橫豎屏切換),黑色view的左、上邊距、大小都不變;
- 灰色view的右邊距不變
- 寬、高、上邊距黑色view相等
案例二
實現:
#import "ViewController2.h"
#import "Masonry.h"
@interface ViewController2 ()
@end
@implementation ViewController2
- (void)viewDidLoad
{
[super viewDidLoad];
UIView *blackView = [UIView new];
blackView.backgroundColor = [UIColor blackColor];
[self.view addSubview:blackView];
// 給黑色view新增約束
[blackView mas_makeConstraints:^(MASConstraintMaker *make) {
// 新增大小約束 make.size.mas_equalTo(CGSizeMake(100, 100));
// 新增左、上邊距約束(左、上約束都是20)
make.left.and.top.mas_equalTo(20);
}];
// 初始化灰色view UIView *grayView = [UIView new];
grayView.backgroundColor = [UIColor lightGrayColor];
[self.view addSubview:grayView];
// 給灰色view新增約束
[grayView mas_makeConstraints:^(MASConstraintMaker *make) {
// 大小、上邊距約束與黑色view相同
make.size.and.top.equalTo(blackView);
// 新增右邊距約束
(這裡的間距是有方向性的,左、上邊距約束為正數,右、下邊距約束為負數)
make.right.mas_equalTo(-20);
}];
}
@end
在上面的案例中,涉及以下內容:
-
在Masonry中,and,with都沒有具體操作,僅僅是為了提高程式的可讀性
make.left.and.top.mas_equalTo(20);
等價於
make.left.top.mas_equalTo(20); -
equalTo與mas_equalTo
如果約束條件是數值或者結構體等型別,可以使用mas_equalTo進行包裝。關於這個問題,我也不是很清楚,可以看看官方的解釋:Instead of using NSNumber, you can use primitives and structs to build your constraints.By default, macros which support autoboxing are prefixed with mas_. Unprefixed versions are available by defining MAS_SHORTHAND_GLOBALS before importing Masonry.
我一般將數值型別的約束用mas_equalTo,而相對於某個控制元件,或者某個控制元件的某個約束,我會使用equalTo,如:
make.size.mas_equalTo(CGSizeMake(100, 100));make.center.equalTo(weakSelf.view);
案例三:
要求:
- 有兩個view,黑色與灰色;
- 黑色view的左、上、右邊距均為20,下邊距灰色view 20,寬度自適應,高度與灰色view平分整個介面;
- 灰色view寬度為黑色view的一半(即左邊以中線起始),右、下邊距與黑色view相同,高度與黑色view相同。
案例三
實現:
#import "ViewController3.h"
#import "Masonry.h"
@interface ViewController3 ()
@end
@implementation ViewController3
- (void)viewDidLoad
{
[super viewDidLoad];
UIView *blackView = [UIView new];
blackView.backgroundColor = [UIColor blackColor];
[self.view addSubview:blackView];
// 給黑色view新增約束
[blackView mas_makeConstraints:^(MASConstraintMaker *make) {
// 新增左、上邊距約束 make.left.and.top.mas_equalTo(20);
// 新增右邊距約束
make.right.mas_equalTo(-20); }];
view UIView *grayView = [UIView new];
grayView.backgroundColor = [UIColor lightGrayColor];
[self.view addSubview:grayView];
// 給灰色view新增約束
[grayView mas_makeConstraints:^(MASConstraintMaker *make) {
// 新增右、下邊距約束 make.bottom.and.right.mas_equalTo(-20);
// 新增高度約束,讓高度等於blackview
make.height.equalTo(blackView);
// 新增上邊距約束(上邊距 = 黑色view的下邊框 + 偏移量20)
make.top.equalTo(blackView.mas_bottom).offset(20);
// 新增左邊距(左邊距 = 父容器縱軸中心 + 偏移量0)
make.left.equalTo(weakSelf.view.mas_centerX).offset(0); }];
}
@end
案例四:
要求
:
當鍵盤擋住輸入框時,輸入框自動向上彈到鍵盤上方。實現
:
這裡需要使用到Masonry的另外一個方法mas_updateConstraints
。這個方法用於更新控制元件約束。
具體的實現方式可以下載Demo來看,這裡只貼出鍵盤彈出時的處理程式碼:
- (void)keyboardWillChangeFrameNotification:(NSNotification *)notification {
// 獲取鍵盤基本資訊(動畫時長與鍵盤高度)
NSDictionary *userInfo = [notification userInfo];
CGRect rect =
[userInfo[UIKeyboardFrameBeginUserInfoKey] CGRectValue];
CGFloat keyboardHeight = CGRectGetHeight(rect);
CGFloat keyboardDuration =
[userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
// 修改下邊距約束
[_textField mas_updateConstraints:^(MASConstraintMaker *make) {
make.bottom.mas_equalTo(-keyboardHeight); }];
// 更新約束
[UIView animateWithDuration:keyboardDuration animations:^{
[self.view layoutIfNeeded]; }];
}
總結:
- 可以給控制元件新增left/right/top/bottom/size/height/width/insert約束;
- 庫提供了三個方法,mas_makeConstraints新增約束,mas_updateConstraints修改約束,mas_remakeConstraints清除以前約束並新增新約束;
- 可以通過view.mas_bottom獲得view的某個約束;
- 在約束的block中,使用make來給當前控制元件新增約束。