您的位置:首页 > 其它

如何让view随着键盘移动

2015-11-23 20:29 495 查看
常见的一个功能,让控件随着Keyboard上下移动而移动,实现方法很多,下面是一个比较方便的方法:

#pragma mark - 键盘改动的时候其他view随着变化
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardHide:) name:UIKeyboardWillHideNotification object:nil];

}

-(void)keyboardShow:(NSNotification *)note
{
NSLog(@"show");
CGRect keyBoardRect=[note.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
CGFloat deltaY=keyBoardRect.size.height;
[UIView animateWithDuration:[note.userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue] animations:^{
self.yourview=CGAffineTransformMakeTranslation(0, -deltaY);
}];
}
-(void)keyboardHide:(NSNotification *)note
{
NSLog(@"hide");
[UIView animateWithDuration:[note.userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue] animations:^{
self.yourview = CGAffineTransformIdentity;
}];
}//点击返回键
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  键盘移动