#import "ViewController.h"
#import "Masonry.h"
@interface ViewController ()
@property(nonatomic,strong)UITextField *greenTextView;
@property(nonatomic,strong)UIButton *blueBtn;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//初始化子控件
[self setupSubviews];
//键盘弹出监听
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
//键盘收回监听
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHidden:) name:UIKeyboardWillHideNotification object:nil];
}
/**
初始化子控件
*/
-(void)setupSubviews
{
//文本框textview
UITextField *greenTextView=[[UITextField alloc]init];
greenTextView.backgroundColor = [UIColor greenColor];
greenTextView.placeholder = @"点我更新约束啦...";
self.greenTextView = greenTextView;
[self.view addSubview:greenTextView];
//设置初始时的约束
//左边距离self.view左边10
//底部距离self.view 20
//高度为50
//宽度自适应
[greenTextView mas_makeConstraints:^(MASConstraintMaker *make) {
make.leading.equalTo(self.view.mas_leading).offset(10);
make.bottom.equalTo(self.view.mas_bottom).offset(-20);
make.height.mas_equalTo(50);
}];
//按钮
UIButton *blueBtn = [[UIButton alloc]init];
blueBtn.backgroundColor = [UIColor blueColor];
self.blueBtn = blueBtn;
[self.view addSubview:blueBtn];
//设置初始时的约束
//右边距离self.view右边10
//左边距离self.greenTextView右边 10
//宽高和self.greenTextView一样
[blueBtn mas_makeConstraints:^(MASConstraintMaker *make) {
make.trailing.equalTo(self.view.mas_trailing).offset(-10);
make.leading.equalTo(self.greenTextView.mas_trailing).offset(10);
make.bottom.equalTo(self.greenTextView.mas_bottom);
make.size.equalTo(self.greenTextView);
}];
}
//键盘弹出时会调用
-(void)keyboardWillShow:(NSNotification *)notification
{
//获取键盘的基本信息
NSDictionary *userInfo = [notification userInfo];
CGRect rect = [userInfo[UIKeyboardFrameBeginUserInfoKey] CGRectValue];
CGFloat keyboardHeight = CGRectGetHeight(rect);
CGFloat duration = [userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
//修改下边距约束
[self.greenTextView mas_updateConstraints:^(MASConstraintMaker *make) {
make.bottom.mas_equalTo(-keyboardHeight-20);
}];
//更新约束
[UIView animateWithDuration:duration animations:^{
[self.view layoutIfNeeded];
}];
}
//键盘收回时会调用
-(void)keyboardWillHidden:(NSNotification *)notification
{
NSDictionary *userInfo = [notification userInfo];
CGFloat duration = [userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
//修改下边距约束
[self.greenTextView mas_updateConstraints:^(MASConstraintMaker *make) {
make.bottom.mas_equalTo(-20);
}];
//更新约束
[UIView animateWithDuration:duration animations:^{
[self.view layoutIfNeeded];
}];
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
//结束编辑
[self.view endEditing:YES];
}
Masonry 更新约束,实现动画效果
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- [self.view addSubview:siftView]; // siftView中含有子view, 此时在...
- Masnory约束UIScrollerView 直接用Masonry对scrollView进行约束,scrollV...
- 前几天发现mansonry更新约束做动画的时候非常不好使,最后折中想了一个方法1.首先需要一个dataArray的...
- 笔记风格借鉴Knight_SJ 需求点: 通过触发键盘实现更新视图约束的动画效果,网上有很多通过Masonry实现...