本文介绍了在UITextField上点击时,将UIButton移动到键盘上方的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有3个文本字段和2个按钮。当我点击文本字段时,键盘出现,并且由于键盘而隐藏了2个按钮。是否可以移动键盘上方的按钮,当键盘退出时,按钮应该回到初始位置。
I have 3 text-fields and 2 buttons. When I tap on text-field, keyboard comes up and the 2 buttons are hidden because of the keyboard. Is it possible to move the buttons above the keyboard and when the keyboard resigns the buttons should go back to their initial position.
推荐答案
//Declare a delegate, assign your textField to the delegate and then
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil];
return YES;
}
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidHide:) name:UIKeyboardDidHideNotification object:nil];
[self.view endEditing:YES];
return YES;
}
- (void)keyboardDidShow:(NSNotification *)notification
{
// Assign new frame to your view
[self.view setFrame:CGRectMake(0,-110,320,460)]; //here taken -20 for example i.e. your view will be scrolled to -20. change its value according to your requirement.
}
-(void)keyboardDidHide:(NSNotification *)notification
{
[self.view setFrame:CGRectMake(0,0,320,460)];
}
编辑: - 使用[UIScreen mainScreen]界限为视图指定宽度和高度] .size
Edit :- Assign width and height to View using [UIScreen mainScreen] bounds].size
这篇关于在UITextField上点击时,将UIButton移动到键盘上方的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!