我一直在寻找出解决方案,但还没有找到一个到目前为止,请参考答案这个,我想知道是否有一种方法,我可以对所有UITextFields执行,而不必硬编码值的检查,感谢帮助。
首先,请确保所有textField的委托都设置为self(表示您的viewController)
Ex. [myTextField setDelegate:self];// you can also set the delegate in Storyboard or xib directly
然后在您的类实现中添加实例变量,例如-
@implementation myViewController
{
UITextField *activeField;
}
然后只需实现以下方法
在您的textFieldShouldBeginEditing中,设置activeField
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
activeField = textField; // HERE get reference of your active field
return true;
}
提供了一个非常好的方法来处理所有问题
CGRect包含Rect
if (!CGRectContainsRect(viewableAreaFrame, activeTextFieldFrame))
{
/*Scroll or move view up*/
}
在您的keyboardWillShow方法中实现以下内容
前任。
- (void)keyboardWillShow:(NSNotification *)notification
{
CGSize keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
float viewWidth = self.view.frame.size.width;
float viewHeight = self.view.frame.size.height;
CGRect viewableAreaFrame = CGRectMake(0.0, 0.0, viewWidth, viewHeight - keyboardHeight);
CGRect activeTextFieldFrame = [activeTextField frame];
if (!CGRectContainsRect(viewableAreaFrame, activeTextFieldFrame))
{
/*Scroll or move view up*/
[UIView animateWithDuration:0.3 animations:^{
CGRect f = self.view.frame;
f.origin.y = -keyboardSize.height;
self.view.frame = f;
}];
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句