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.
Asked
Active
Viewed 194 times
0
-
you can loop through all the textfields as subviews of the superview. – Nishant Mar 02 '16 at 05:11
-
use IBoutletcollection , it is easy to check – Anbu.Karthik Mar 02 '16 at 05:14
-
@Nishant doesn't sound like a good idea, when the check has to be performed on that particular UITextField only. – Mar 02 '16 at 05:16
-
you can also try this `[self.view viewWithTag:33];` You will have to set tag to all the textfields. and Avoid 0. – Nishant Mar 02 '16 at 05:18
1 Answers
0
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;
}];
}
}

gunjot singh
- 2,578
- 20
- 28
-
that was great help, can you help me with setting a UITextfield as active, sorry i am a newbie. – Mar 02 '16 at 05:37
-
-
Is there an alternative to `CGRectContainsRect`, because my `viewableAreaFrame` has to be the frame minus the `keyboardSie.height`, when i do that directly i am initialising CGRect with double. (error) – Mar 02 '16 at 06:13
-
Updated my answer again, to calculate effective viewableAreaFrame – gunjot singh Mar 02 '16 at 06:37
-
I got that working, i first calculated and stored the height of viewableAreaFrame in a `CGFloat`, then used that height to create a `CGRect`. After this the `if` expression works as expected. – Mar 02 '16 at 06:37