Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIScrollView scrolling automatically by 64 points

I'm adding a UIScrollView to a UIViewControllers view. For some reason, between adding the scroll view to the view and it getting displayed, the contentOffset is set to {0, -64}, 64 being the status bar's 20 plus the navigation bar's 44 points (I guess). Below is some code that reproduces the issue, and an image.

How do I prevent iOS from setting the contentOffset?


- (void)viewDidLoad
{
    [super viewDidLoad];

    _scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(10, 100, 100, 100)];
    _scroll.backgroundColor = [UIColor greenColor];
    _scroll.delegate = self;

    UIView *red = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 10)];
    red.backgroundColor = [UIColor redColor];
    [_scroll addSubview:red];

    [self.view addSubview:_scroll];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];

    // outputs {0, -64}
    NSLog(@"%@", NSStringFromCGPoint(_scroll.contentOffset));
}

Screenshot

like image 516
fabian789 Avatar asked Dec 05 '13 20:12

fabian789


People also ask

How do I stop UIScrollView scrolling?

For disabling the same we need to make the “isScrollEnabled” property of our scroll view to false. Copy the below code in your file. import UIKit class ViewController: UIViewController { @IBOutlet var scrollView: UIScrollView! override func viewDidLoad() { super.

How do I make my UI view scrollable?

You cannot make a UIView scrollable. That's what UIScrollView is for. However if you are using storyboards you can try to add constraints to the view so when you rotate the device the content remains inside the viewable area. Sounds like you already have some constraints setup so I would just play around with them.

How do you make a scrollable view in Swift?

One way to do this is programmatically create an UIScrollView in your UIViewController . To control the scrollability you can set the ScrollView contentSize property.

How do I scroll up when keyboard appears in IOS?

Handling the Keyboard Appearing Notification There are two things to do in keyboardWasShown to scroll the text view. First, set the text view's content inset so the bottom edge is the keyboard's height. Second, set the scroll indicator insets to the text view's content inset.


1 Answers

Set automaticallyAdjustsScrollViewInsets on your view controller to NO, otherwise it'll adjust insets on the first subview of it's root view that happens to be of UIScrollView class.

More on this in iOS 7 Transition Guide.

like image 117
Dmitry Shevchenko Avatar answered Sep 22 '22 10:09

Dmitry Shevchenko