0

I am on Xcode 8.3.3, OSX not iOS.

I have a customView I use as container to replace other views inside it. The dark blue box on the right represents the container:

enter image description here

The constraints of the container are set in IB as follows where "scrollView" is the tables scroll view left of it:

enter image description here

This enables vertical scaling (scrollView is allowed to scale vertically) which is the desired behaviour:

enter image description here

Now I add a subview to the container with this code:

NSStoryboard *storyBoard = [NSStoryboard storyboardWithName:@"Main" bundle:nil];
    _previewViewController = [storyBoard instantiateControllerWithIdentifier:@"showSHPreviewViewController"];

CGRect newFrame = CGRectMake(0, 0,CGRectGetWidth(_previewViewController.view.bounds),CGRectGetHeight(_previewViewController.view.bounds));
_previewViewController.view.frame = newFrame;

[self.previewContainer addSubview:_previewViewController.view];

This works as expected:

enter image description here

My Problem is, I want the subview to be stretched so it fits the whole height of its container. Therefore I have to set NSLayoutConstraints programmatically. But i don't get the logic.... Whatever I try leads to a behaviour where neither the scroll view nor the container can scale vertically at all.

This was my last try before I wrote this post:

[self.previewContainer addConstraint:[NSLayoutConstraint
    constraintWithItem:_previewViewController.view
    attribute:NSLayoutAttributeTop
    relatedBy:NSLayoutRelationEqual
    toItem:self.previewContainer
    attribute:NSLayoutAttributeTop
    multiplier:1.0
    constant:0]];

[self.previewContainer addConstraint:[NSLayoutConstraint constraintWithItem:_previewViewController.view
    attribute:NSLayoutAttributeLeft
    relatedBy:NSLayoutRelationEqual
    toItem:self.previewContainer
    attribute:NSLayoutAttributeLeft
    multiplier:1.0
    constant:0]];

[self.previewContainer addConstraint:[NSLayoutConstraint constraintWithItem:_previewViewController.view
    attribute:NSLayoutAttributeBottom
    relatedBy:NSLayoutRelationEqual
    toItem:self.previewContainer
    attribute:NSLayoutAttributeBottom
    multiplier:1.0
    constant:0]];

I can confirm that the content of the added subview is setup correctly and resizeable (no "height-lock").

Cœur
  • 37,241
  • 25
  • 195
  • 267
Pat_Morita
  • 3,355
  • 3
  • 25
  • 36
  • Are you setting `translatesAutoresizingMaskIntoConstraints` to `false` on the view that your placing via Auto Layout? The Xcode console will print layout errors if this is the case. On the other hand, I think you should be using the UIViewController containment APIs to add a view controllers view as a subview. – paulvs Jun 20 '17 at 22:19
  • 1
    thanks for your Feedback. i dont set that property at all. And i dont have any UI classes as i am on osx not ios – Pat_Morita Jun 20 '17 at 22:22

0 Answers0