Masonry是自动布局的一个第三方库,首先在项目工程中导入,用Cocoapods,在终端下,cd到项目文件,创建Podfile文件。
在这里,说一下Podfile文件的写法(因为自己也不是很熟悉,多写几次就熟悉了):
platform :ios, '9.0'
use_frameworks!
target 'test' do
pod 'Mantle'
pod 'YYModel'
pod 'Masonry'
pod 'ReactiveObjC'
end
简单的就是这样,负载的会在项目中会有多个target就写多个target,不同的target导入不同的库。
然后pod install就好了。
在这里,我们是用的是Masonry,基本方法有相应的文档,在这里主要是自己记录一下自己学习键盘弹出隐藏时界面的布局。
自动布局.gif以下是代码:
/*------------textfiled键盘适配----------*/
//定义一个textfiled,首先将它add到view上,在进行约束
_textField = [UITextField new];
_textField.backgroundColor = [UIColor yellowColor];
[self.view addSubview:_textField];
//在定义一个对比的View,让他一起适配
_uiView = [[UIView alloc] init];
_uiView.backgroundColor = [UIColor redColor];
[self.view addSubview:_uiView];,
[_textField mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(20);
make.centerX.equalTo(self.view);
make.height.mas_equalTo(40);
make.bottom.mas_equalTo(0);
}];
[_uiView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(self.view.mas_left).offset(100);
make.top.mas_equalTo(self.view.mas_top).offset(100);
make.right.mas_equalTo(self.view.mas_right);
make.bottom.mas_equalTo(self.view.mas_bottom).offset(-400);
}];
这样首先将我的界面添加完毕,然后将我的键盘注册到通知中心
//注册键盘通知
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardWillChangeFrameNotification:) name:UIKeyboardWillChangeFrameNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHideNotification:) name:UIKeyboardWillHideNotification object:nil];
接着就是重点部分了,在代理方法中通过传入的notification参数来获取键盘弹出和隐藏时的高度。
/**
键盘弹出代理方法
@param notification <#notification description#>
*/
-(void)keyboardWillChangeFrameNotification:(NSNotification *)notification{
NSLog(@"-----%@",notification);
//获取notification的信息
NSDictionary * userInfo = [notification userInfo];
//UIKeyboardFrameEndUserInfoKey方法是用来获取键盘弹出后的高度,宽度
CGRect rect = [userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
CGFloat keyboardHeight = CGRectGetHeight(rect);
NSLog(@"%f",keyboardHeight);
//这个参数是动画时的速度
CGFloat keyboardDuration = [userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
这是打印出来的结果,在userInfo中找自己要用的数据
打印结果.png
//界面组件的约束进行更新部署
[_uiView mas_updateConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(self.view.mas_top).offset(100-keyboardHeight);
make.bottom.mas_equalTo(self.view.mas_bottom).offset(-keyboardHeight-400);
}];
[_textField mas_updateConstraints:^(MASConstraintMaker *make) {
//textFiled进行向上挪动键盘高度那么高
make.bottom.mas_equalTo(self.view.mas_bottom).offset(-keyboardHeight);
}];
// 更新约束
[UIView animateWithDuration:keyboardDuration animations:^{
[self.view layoutIfNeeded];
}];
}
/**
键盘将要隐藏的代理方法
@param notification <#notification description#>
*/
- (void)keyboardWillHideNotification:(NSNotification *)notification{
NSLog(@"~~~~~~%@",notification);
// 获得键盘动画时长
NSDictionary *userInfo = [notification userInfo];
CGFloat keyboardDuration = [userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
// 修改为以前的约束(距下边距0)
[_uiView mas_updateConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(self.view.mas_left).offset(100);
make.top.mas_equalTo(self.view.mas_top).offset(100);
make.right.mas_equalTo(self.view.mas_right);
make.bottom.mas_equalTo(self.view.mas_bottom).offset(-400);
}];
[_textField mas_updateConstraints:^(MASConstraintMaker *make) {
make.bottom.mas_equalTo(0);
}];
// 更新约束
[UIView animateWithDuration:keyboardDuration animations:^{
[self.view layoutIfNeeded];
}];
}
//点击屏幕其他地方键盘收回
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesBegan:touches withEvent:event];
[self.view endEditing:YES];
}
以上就是所有的实现。
网友评论