Note that there are some explanatory texts on larger screens.

plurals
  1. POUIScrollView showing only part of UIView after rotation
    primarykey
    data
    text
    <p>This is my first post to Stack Overflow, and I'm a iOS beginner, so please bear with me!</p> <p>I have an example app where I have three UIViews (headerView, scrollViewContainer, and bodyView) in a parent UIView (topView), and all of these views are created in code. The topView is added to a UIScrollView (pageScrollView) created in storyboard.</p> <p>pageScrollView is filling the full screen of an iPhone, and I used Autolayout. The app only contains the ViewController.h and companioning .m-file seen below, plus Appdelegate.x. I think I used the single view application template to start with. I'm using iOS 6 and Xcode 4.6, but I also tried 4.5.</p> <p>I've tried to remove as much as possible of other code that's irrelevant to this problem.</p> <p>The problem: When the app starts it shows all its views correctly, and the scrollView allows to view all three views as intended. But after rotating to landscape, the scrollView somehow offsets the content. For example: Stay at top and rotate = content looks OK, but scroll down a bit and rotate makes the top of the content not to show.</p> <p>What I have tried: I've searched the net for help, but I haven't found anything that seemed to help me. I've added logging of various data like origin, and contentSize, and also tried to set some of them but without any success. I also used 'po [[UIWindow keyWindow] _autolayoutTrace]' to ensure that the constraints are OK.</p> <p>I can't see what I'm doing wrong. Are there any obvious things missing in my code?</p> <p>Thanks in advance!</p> <p>Here's the ViewController.m:</p> <pre><code>#import "ViewController.h" @interface ViewController () { UIView *topView; UIView *headerView; UIView *bodyView; UIView *scrollViewContainer; UIInterfaceOrientation newOrientation; CGFloat bodyViewHeight; CGSize newBounds; float pictureScrollHeight; } @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; newBounds = [self sizeInOrientation:[UIApplication sharedApplication].statusBarOrientation]; newOrientation = [UIApplication sharedApplication].statusBarOrientation; bodyViewHeight = 1200; // The height of the body view varies in size depending on orientation self.pageScrollView.translatesAutoresizingMaskIntoConstraints = NO; topView = [[UIView alloc] init]; [topView setBackgroundColor:[UIColor clearColor]]; topView.translatesAutoresizingMaskIntoConstraints = NO; [self.pageScrollView addSubview:topView]; headerView = [[UIView alloc] init]; [headerView setBackgroundColor:[UIColor redColor]]; headerView.translatesAutoresizingMaskIntoConstraints = NO; [topView addSubview:headerView]; scrollViewContainer = [[UIView alloc] init]; [scrollViewContainer setBackgroundColor:[UIColor blueColor]]; scrollViewContainer.translatesAutoresizingMaskIntoConstraints = NO; [topView addSubview:scrollViewContainer]; bodyView = [[UIView alloc] init]; [bodyView setBackgroundColor:[UIColor greenColor]]; bodyView.translatesAutoresizingMaskIntoConstraints = NO; [topView addSubview:bodyView]; [self updateViewConstraints]; } - (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; } - (void)updateViewConstraints { [super updateViewConstraints]; // Remove old constraints [self.view removeConstraints:self.view.constraints]; [self.pageScrollView removeConstraints:self.pageScrollView.constraints]; [topView removeConstraints:topView.constraints]; [scrollViewContainer removeConstraints:scrollViewContainer.constraints]; if ((newOrientation == UIDeviceOrientationLandscapeLeft) || (newOrientation == UIDeviceOrientationLandscapeRight)) { pictureScrollHeight = 300; } else { pictureScrollHeight = 203; } [headerView setNeedsDisplay]; [bodyView setNeedsDisplay]; CGFloat topViewHeight = bodyViewHeight + 55 + pictureScrollHeight; //self.pageScrollView = _pageScrollView NSDictionary *viewsDict = NSDictionaryOfVariableBindings(_pageScrollView, topView, headerView, bodyView, scrollViewContainer); NSDictionary *metricsDict = @{@"topViewHeight": [NSNumber numberWithFloat:topViewHeight], @"newBoundsWidth": [NSNumber numberWithFloat:newBounds.width], @"pictureScrollHeight": [NSNumber numberWithFloat:pictureScrollHeight], @"bodyViewHeight": [NSNumber numberWithFloat:bodyViewHeight]}; // pageScrollView - child to self.view [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[_pageScrollView]-0-|" options:0 metrics:nil views:viewsDict]]; [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[_pageScrollView]-0-|" options:0 metrics:nil views:viewsDict]]; // topView - child to pageScrollView [self.pageScrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[topView(newBoundsWidth)]-0-|" options:0 metrics:metricsDict views:viewsDict]]; [self.pageScrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[topView(topViewHeight)]-0-|" options:0 metrics:metricsDict views:viewsDict]]; // headerView - child to topView [topView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[headerView]-0-|" options:0 metrics:nil views:viewsDict]]; [topView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[headerView(55.0)]" options:0 metrics:nil views:viewsDict]]; // scrollViewContainer - child to topView [topView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[scrollViewContainer]-0-|" options:0 metrics:nil views:viewsDict]]; [topView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[headerView]-0-[scrollViewContainer(pictureScrollHeight)]" options:0 metrics:metricsDict views:viewsDict]]; // bodyView - child to topView [topView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[bodyView]-0-|" options:0 metrics:nil views:viewsDict]]; [topView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[scrollViewContainer]-0-[bodyView(bodyViewHeight)]-0-|" options:0 metrics:metricsDict views:viewsDict]]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration { newOrientation = toInterfaceOrientation; newBounds = [self sizeInOrientation:toInterfaceOrientation]; } -(CGSize) sizeInOrientation:(UIInterfaceOrientation)orientation { CGSize size = [UIScreen mainScreen].bounds.size; UIApplication *application = [UIApplication sharedApplication]; if (UIInterfaceOrientationIsLandscape(orientation)) { size = CGSizeMake(size.height, size.width); } if (application.statusBarHidden == NO) { size.height -= MIN(application.statusBarFrame.size.width, application.statusBarFrame.size.height); } return size; } @end </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload