简体   繁体   中英

Setting contentInset of UIScrollView not working

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM