In my application i have header and banner images included on the top and bottom of the view, and also included a scrollview with more 15 UITextfields.
I used anyW, anyH auto layout and height of parent view is set as freeform with the height of 2500.
I used the below codes to hide and show the keyboard on the screen , it works fine, also used the below code to move the active text field above the key board.
The issue what i am facing here is the current active text filed takes wrong x,y position based on the freeform height of the view.
The below is the Firstname textfield x,y position, but it actually placed on top of the screen with the frame as a(100 250; 270 28);
UITextField: 0x1918a820; frame = (14.5 1405; 270 28); text = ''; clipsToBounds = YES; opaque = NO; autoresize = RM+BM; gestureRecognizers = NSArray: 0x191a5900; layer = CALayer: 0x19189f40
while i starts to enter text in FirstName field, it takes the firstname field frame postion as (14.5 1405; 270 28) and scrolls up position like the second screen.
Now its at Y position 1415. Why my active text field takes wrong x, y positions?
Why my active textfield takes wrong frame position and to fix this issue?
- (void) viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
NSLog(@"Registering for keyboard events");
// Register for the events
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector (keyboardDidShow:) name: UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector (keyboardDidHide:) name: UIKeyboardDidHideNotification object:nil];
// Setup content size
_scrollViewAddContact.contentSize = CGSizeMake(self.view.frame.size.width,_scrollViewAddContact.frame.size.height);
//Initially the keyboard is hidden
keyboardVisible = NO;
}
-(void) viewWillDisappear:(BOOL)animated {
NSLog (@"Unregister for keyboard events");
[[NSNotificationCenter defaultCenter]removeObserver:self];
}
-(BOOL) textFieldShouldBeginEditing:(UITextField*)textField {
activeField = textField;
return YES;
}
-(BOOL) textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
-(void) keyboardDidShow: (NSNotification *)notif {
NSLog(@"Keyboard is visible");
// If keyboard is visible, return
if (keyboardVisible) {
NSLog(@"Keyboard is already visible. Ignore notification.");
return;
}
// Get the size of the keyboard.
NSDictionary* info = [notif userInfo];
NSValue* aValue = [info objectForKey:UIKeyboardFrameEndUserInfoKey];
CGSize keyboardSize = [aValue CGRectValue].size;
// Save the current location so we can restore
// when keyboard is dismissed
offset = _scrollViewAddContact.contentOffset;
// Resize the scroll view to make room for the keyboard
CGRect viewFrame = _scrollViewAddContact.frame;
viewFrame.size.height -= keyboardSize.height;
_scrollViewAddContact.frame = viewFrame;
CGRect textFieldRect = [activeField frame];
textFieldRect.origin.y += 10;
[_scrollViewAddContact scrollRectToVisible:textFieldRect animated:YES];
NSLog(@"ao fim");
// Keyboard is now visible
keyboardVisible = YES;
}
-(void) keyboardDidHide: (NSNotification *)notif {
// Is the keyboard already shown
if (!keyboardVisible) {
NSLog(@"Keyboard is already hidden. Ignore notification.");
return;
}
// Reset the frame scroll view to its original value
_scrollViewAddContact.frame = CGRectMake(_scrollViewAddContact.frame.origin.x, _scrollViewAddContact.frame.origin.y, self.view.frame.size.width, _scrollViewAddContact.frame.size.height);
// Reset the scrollview to previous location
_scrollViewAddContact.contentOffset = offset;
// Keyboard is no longer visible
keyboardVisible = NO;
}