在 UITextField 上点击时将 UIButton 移动到键盘上方
Move UIButton above Keyboard when tapped on UITextField
我有 3 个文本字段和 2 个按钮。当我点击文本字段时,键盘出现并且 2 个按钮因键盘而被隐藏。是否可以将按钮移动到键盘上方,当键盘退出时按钮应该回到它们的初始位置。
//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] bounds].size
为视图分配宽度和高度
是的,您可以更新视图 frames/constraints 以便向用户显示按钮。
此外,您可以创建一个包含这些按钮的工具栏。当文本字段获得焦点时,您可以在键盘上方添加工具栏。
这将是一个更好的用户体验。
为了您的方便,最好使用 scrollview 或修改下面的代码。
override func viewDidLoad() {
super.viewDidLoad()
let tapGesture = UITapGestureRecognizer(target: self, action: "tap:")
view.addGestureRecognizer(tapGesture)
self.txtUserEmail.delegate = self;
self.txtPassword.delegate = self;
}
func tap(gesture: UITapGestureRecognizer) {
self.txtUserEmail.resignFirstResponder()
self.txtPassword.resignFirstResponder()
}
最好的方法是使用库,这里是 link https://github.com/michaeltyson/TPKeyboardAvoiding,您需要做的就是在 UIScrollView 中获取所有文本字段和按钮并分配 scrollview class 作为情节提要中的 "TPKeyboardAvoidingScrollView"。
视图控制器中将有零行代码 class 来管理您的需求,并且您可以在整个应用程序的任何地方进行类似的操作。
我有 3 个文本字段和 2 个按钮。当我点击文本字段时,键盘出现并且 2 个按钮因键盘而被隐藏。是否可以将按钮移动到键盘上方,当键盘退出时按钮应该回到它们的初始位置。
//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] bounds].size
为视图分配宽度和高度是的,您可以更新视图 frames/constraints 以便向用户显示按钮。
此外,您可以创建一个包含这些按钮的工具栏。当文本字段获得焦点时,您可以在键盘上方添加工具栏。
这将是一个更好的用户体验。
为了您的方便,最好使用 scrollview 或修改下面的代码。
override func viewDidLoad() {
super.viewDidLoad()
let tapGesture = UITapGestureRecognizer(target: self, action: "tap:")
view.addGestureRecognizer(tapGesture)
self.txtUserEmail.delegate = self;
self.txtPassword.delegate = self;
}
func tap(gesture: UITapGestureRecognizer) {
self.txtUserEmail.resignFirstResponder()
self.txtPassword.resignFirstResponder()
}
最好的方法是使用库,这里是 link https://github.com/michaeltyson/TPKeyboardAvoiding,您需要做的就是在 UIScrollView 中获取所有文本字段和按钮并分配 scrollview class 作为情节提要中的 "TPKeyboardAvoidingScrollView"。
视图控制器中将有零行代码 class 来管理您的需求,并且您可以在整个应用程序的任何地方进行类似的操作。