Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First of all you do not need to recreate all constraints in -updateViewConstraints method. You need to just update them in this place. To achieve your aim do the following:</p> <ol> <li>Create your constraints only once. For example in method -setupConstraints. And keep reference to those that need to be updated. See code below.</li> <li>In method -updateViewConstraints just update topView height and width constraints and height for scrollViewContainer.</li> </ol> <p>Here's the ViewController.m should look like:</p> <pre><code>#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 </code></pre> <p>ScrollView calculates its content size automatically depending on subviews constraints added into it. Off course it works only in autolayout enviroument.</p>
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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