Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIScrollView contentInset not working

I'm having a problem with contentInset not working for a UIScrollView to work with a keyboard popup. It kind of works: for some reason I need large numbers (maybe above the view's height?) for it to do anything, despite all documentation of contentInset showing small numbers like 40.0 (e.g. for a bar), or the keyboard height.

I've reproduced the problem on a brand new application by the following steps:

  1. Create new single view application using Xcode new project
  2. On storyboard, drag in a scrollview, filling full view size
  3. On storyboard, drag in a button, at the very bottom of the screen (inside the scrollview)
  4. Link the scrollview to a new property in the ViewController
  5. Link button to a method in the ViewController
  6. Make the button's pressed method set the contentInset

Here is the code for the ViewController:

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@end

@implementation ViewController
- (IBAction)button:(id)sender {
    self.scrollView.contentInset = UIEdgeInsetsMake(0.0, 0.0, 600, 0.0);
}
@end

What am I missing / why do I need large numbers?

like image 767
Dan2552 Avatar asked Jan 15 '14 16:01

Dan2552


1 Answers

Check that self.scrollView.contentSize is set properly. If contentSize.height is 0 (as will be the case following your steps), then a large inset is required.

Try adding [self.scrollView setContentSize: CGSizeMake(320, 568)]; to the button method and you'll notice that your inset will now behave as expected.

like image 186
Matt Avatar answered Sep 22 '22 08:09

Matt