问题
I have been looking out for solutions but haven't found one so far, please refer to the answer to this, i want to know if there is a way i can perform the check on all UITextFields, instead of having a hard coded value, thanks for help.
回答1:
first your make sure that all textField have their delegate set to self ( means your viewController)
Ex. [myTextField setDelegate:self];// you can also set the delegate in Storyboard or xib directly
Then add a instance variable in your class implementation like -
@implementation myViewController
{
UITextField *activeField;
}
and then simply implement the method as below
in your textFieldShouldBeginEditing, set activeField
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
activeField = textField; // HERE get reference of your active field
return true;
}
There is a very nice method provided for all handling
CGRectContainsRect
if (!CGRectContainsRect(viewableAreaFrame, activeTextFieldFrame))
{
/*Scroll or move view up*/
}
Implement below in your keyboardWillShow method
EX.
- (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;
}];
}
}
来源:https://stackoverflow.com/questions/35738921/how-can-i-perform-a-check-on-all-check-uitextfields-in-if-condition