Reputation: 401
I am using Apple's proposed way of revealing a UITextfield, which gets hidden by the keyboard when selected (see listing 5-1): https://developer.apple.com/library/ios/documentation/StringsTextFonts/Conceptual/TextAndWebiPhoneOS/KeyboardManagement/KeyboardManagement.html#//apple_ref/doc/uid/TP40009542-CH5-SW1
This is not working for me though because every time I set the conentInset my scrollView automatically scrolls up just a few pixels and thats it. Even if I remove the scrollRectToVisible call my scrollView still moves up. If I set the contentInset in viewDidLoad everything works as excepted.
Note: I am currently not using autolayout. If I turn autolayout on nothing happen at all.
Any suggestions?
Upvotes: 1
Views: 2652
Reputation: 31311
Set the contentInset
,setContentOffset
and scrollIndicatorInsets
Example:
/*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Method is used to handle the keyboard visibility.
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
- (void)keyboardWasShown:(NSNotification *)notification
{
@try
{
// Step 1: Get the size of the keyboard.
CGSize keyboardSizePotriat = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
CGSize keyboardSize = {keyboardSizePotriat.height,keyboardSizePotriat.width};
// Step 2: Adjust the bottom content inset of your scroll view by the keyboard height.
UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, keyboardSize.height, 0.0);
self.theScrollView.contentInset = contentInsets;
self.theScrollView.scrollIndicatorInsets = contentInsets;
// Modify the scrollPoint as per your screen.
CGPoint scrollPoint = CGPointMake(0.0, self.activeTextField.frame.origin.y - (keyboardSize.height - 45));
[self.theScrollView setContentOffset:scrollPoint animated:YES];
}
@catch (NSException *exception)
{
NSLog(@"%s\n exception: Name- %@ Reason->%@", __PRETTY_FUNCTION__,[exception name],[exception reason]);
}
}
Check out the link below for more details
iOS SDK: Keeping Content From Underneath the Keyboard
Upvotes: 2