3

Simply adding a subview UIView from a controller creating using instantiateViewControllerWithIdentifier to the current controllers view and adding constraints to maintain the size of the new subview to cover the entire area. The code below worked in iOS 7. iOS 8 sizes the subview to a small portion of the upper left corner. iOS 7 does what I expect, which is to size the subview across the entire size of the parent view. I'd attach an image, but don't have the rep for that. Setting translatesAutoresizingMaskIntoConstraints to YES fixes the issue, but then the view does not honor the constraints and resize when orientation changes or sizing changes.

spotCheckStoresViewController = [self.storyboard     instantiateViewControllerWithIdentifier:@"visitStoresViewController"];
spotCheckStoresViewController.mainViewController = self;
spotCheckStoresViewController.dataMode = kDataModeViews;
spotCheckStoresViewController.lastRow = [self getViewsLastRow];
spotCheckStoresViewController.view.tag = -200;

currentView = spotCheckStoresViewController.view;

[self addChildViewController:spotCheckStoresViewController];

[self.view insertSubview:currentView belowSubview:_menuViewController.view];

currentView.translatesAutoresizingMaskIntoConstraints = NO;

[self.view addConstraint:[NSLayoutConstraint constraintWithItem:currentView attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeading multiplier:1.0 constant:0.0]];

[self.view addConstraint:[NSLayoutConstraint constraintWithItem:currentView attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTrailing multiplier:1.0 constant:0.0]];

[self.view addConstraint:[NSLayoutConstraint constraintWithItem:currentView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1.0 constant:0.0]];

[self.view addConstraint:[NSLayoutConstraint constraintWithItem:currentView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0.0]];

//[self.view updateConstraints];
//[self.view layoutIfNeeded];

I've tried setting the frame of the subview as well. Any ideas what might have changed in iOS 8 to alter the behavior of constraints used in this way?

vikingosegundo
  • 52,040
  • 14
  • 137
  • 178
bavelasq
  • 31
  • 4
  • I've fixed this for myself by adding another sub UIView to the controllers main UIView in IB, setting constraints on that to stick to the leading, trailing, top and bottom, and then adding my original sub-view to that child UIView. Seems to do the job. – bavelasq Sep 12 '14 at 22:22
  • in iOS 8 a couple new layout guides were added, ```leftLayoutGuide``` and ```rightLayoutGuide```. you might try setting your constraints to those, but remember, that is not available prior to iOS 8. – Andrew Monshizadeh Sep 12 '14 at 22:26
  • This is not relevant to your question, but you are forgetting to call `didMoveToParentViewController:`. – matt Sep 12 '14 at 22:27
  • @AndrewMonshizadeh There is no such thing as `leftLayoutGuide` and `rightLayoutGuide`. There are margins in iOS 8, but that's irrelevant; a manually constructed constraint does not pin to the margin. – matt Sep 12 '14 at 22:28
  • @bavelasq _When_ does the above code run, please? i.e. what is the surrounding method? – matt Sep 12 '14 at 22:32
  • @matt interesting, searching the xcode documentation still finds those guide names, but then viewing the view controller reference they do not appear. my bad. – Andrew Monshizadeh Sep 12 '14 at 22:40
  • @AndrewMonshizadeh I presume what happened is they changed their minds and went with margins instead. :) – matt Sep 12 '14 at 22:52
  • Likely. Too bad as they started down that road with the top and bottom guides. – Andrew Monshizadeh Sep 13 '14 at 01:12
  • I found this, which helped me resolve my problem. I don't know if it applies to your situation, but the iPad height/width attributes are now based on the orientation of the device http://stackoverflow.com/questions/24150359/is-uiscreen-mainscreen-bounds-size-becoming-orientation-dependent-in-ios8 – Chris Oct 28 '14 at 21:44

2 Answers2

0

In my case, the problem related to constraints labeled UIView-Encapsulated-Layout-Width and UIView-Encapsulated-Layout-Height. When I removed them, everything behaved as though my view was of zero size, with everything centered on the upper left corner of the screen. When I left them in, new constraints worked as expected. I also retained the constraints labeled _UILayoutSupportConstraint.

Tad
  • 4,668
  • 34
  • 35
0

In my case, it was only happening on iOS 8.3. There was a conflict with existing constraints. A colleague found that I needed to first remove any existing constraints before adding the others.

[self.view removeConstraints:self.constraints];
Matt H
  • 6,422
  • 2
  • 28
  • 32