UIScrollView only shows part of UIView after rotation

This is my first post on Stack Overflow and I am starting iOS, so please bear with me!

I have a sample 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 the UIScrollView (pageScrollView) created in the storyboard.

pageScrollView fills iPhone full screen and I used Autolayout. The application contains only ViewController.h and the accompanying .m file shown below, plus Appdelegate.x. I think I used a single view app template to start with. I am using iOS 6 and Xcode 4.6, but I tried 4.5 as well.

I tried to remove as much other code as possible that is not relevant to this issue.

Problem: When the app starts, it displays all of its views correctly, and the scrollView allows all three views to be viewed as intended. But after turning to landscape, the scrollView shifts the content somehow. For example: Stay at the top and rotate = the content looks fine, but scroll and rotate a little to avoid showing the top of the content.

What I tried: I searched the net for help, but I couldn't find anything that could help me. I have added logging of various data such as origin and contentSize, and also tried to set some of them, but without any success. I also used 'po [[UIWindow keyWindow] _autolayoutTrace]' to ensure the constraints are in order.

I can't see what I am doing wrong. Are there any obvious things in my code?

Thanks in advance!

Here's ViewController.m:

#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

      

+3


source to share


3 answers


First of all, you don't need to recreate all the constraints in the -updateViewConstraints method. You just need to update them at this location. To achieve your goal, do the following:

  • Create your constraints only once. For example, in the -setupConstraints method. And keep a link to the ones that need to be updated. See code below.
  • In the -updateViewConstraints method, it is easy to update the top and vertical height and width constraints for the scrollViewContainer.

Here ViewController.m should look like this:



#import "ViewController.h"

@interface ViewController ()
@property (nonatomic, strong) IBOutlet UIScrollView* pageScrollView;
@property (nonatomic, strong) NSLayoutConstraint* pictureHeightConstraint;
@property (nonatomic, strong) NSLayoutConstraint* topViewWidthConstraint;
@property (nonatomic, strong) NSLayoutConstraint* topViewHeightConstraint;

@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 setupConstraints];
}


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

- (void)setupConstraints {
    // 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);

    // 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]-0-|" options:0 metrics:nil views:viewsDict]];
    [self.pageScrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[topView]-0-|" options:0 metrics:nil views:viewsDict]];
    NSLayoutConstraint* topViewWidthConstraint = [NSLayoutConstraint constraintWithItem:topView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:newBounds.width];
    self.topViewWidthConstraint = topViewWidthConstraint;
    [topView addConstraint:self.topViewWidthConstraint];
    NSLayoutConstraint* topViewHeightConstraint = [NSLayoutConstraint constraintWithItem:topView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:topViewHeight];
    self.topViewHeightConstraint = topViewHeightConstraint;
    [topView addConstraint:self.topViewHeightConstraint];

    // 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]" options:0 metrics:nil views:viewsDict]];
    NSLayoutConstraint* pictureHeightConstraint = [NSLayoutConstraint constraintWithItem:scrollViewContainer attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:pictureScrollHeight];
    self.pictureHeightConstraint = pictureHeightConstraint;
    [scrollViewContainer addConstraint:self.pictureHeightConstraint];

    // 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]-0-|" options:0 metrics:nil views:viewsDict]];
}

- (void)updateViewConstraints
{
    [super updateViewConstraints];
    if ((newOrientation == UIDeviceOrientationLandscapeLeft) || (newOrientation == UIDeviceOrientationLandscapeRight)) {
        pictureScrollHeight = 300;
    } else {
        pictureScrollHeight = 203;
    }

    CGFloat topViewHeight = bodyViewHeight + 55 + pictureScrollHeight;
    self.pictureHeightConstraint.constant = pictureScrollHeight;
    self.topViewHeightConstraint.constant = topViewHeight;
    self.topViewWidthConstraint.constant = newBounds.width;
    [self.pageScrollView setNeedsUpdateConstraints];
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
    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

      

The ScrollView will automatically size its content based on the subviews constraints added to it. Of course it only works in autorun mode.

+1


source


Check out the auto resizing masks.



[self.pageScrollView setAutoresizingMask:UIViewAutoresizingFlexibleHeight];

      

0


source


It looks like you are not doing anything with the newBounds in willRotateToInterfaceOrientation. Shouldn't the updateView method be called after getting new bounds.

0


source







All Articles