0
votes

In my app i have two top level UIViews i created in IB. One is the UIViewControllers view, it has a subview which is a UIScrollView. The other top level UIView is the content that's going to go in the UIScrollView. That part works fine, i set the UIScrollView's contentsize to the the other UIView's size, and everything works perfectly in portrait mode. However if i switch to landscape mode, the UIScrollView resizes nicely, but its subview stays in the top-left corner, but i want it to be in the horizontal center. So i created this constraint and added it to the UIScrollView:

  NSLayoutConstraint* iPhoneTextFieldContainerAlwaysInHorizontalCenter = [NSLayoutConstraint
                                                                                constraintWithItem:self.iPhoneTextFieldContainerView
                                                                                attribute:NSLayoutAttributeCenterX
                                                                                relatedBy:NSLayoutRelationEqual
                                                                                toItem:self.iPhoneMainScrollView
                                                                                attribute:NSLayoutAttributeCenterX
                                                                                multiplier:1.0
                                                                            constant:0];

However when i switch to landscape mode, the get this constraint exception:

( NSAutoresizingMaskLayoutConstraint:0x76dacb0 h=-&- v=-&- UIView:0x76d6eb0.width == UIViewControllerWrapperView:0x76d5b10.width, NSAutoresizingMaskLayoutConstraint:0x76d7e70 h=--& v=--& UIView:0x76d3110.midX == + 160, NSLayoutConstraint:0x76d7030 UIScrollView:0x7680f10.trailing == UIView:0x76d6eb0.trailing, NSLayoutConstraint:0x76d6ff0 H:|-(0)-[UIScrollView:0x7680f10]

(Names: '|':UIView:0x76d6eb0 ), NSLayoutConstraint:0x76d7330 UIView:0x76d3110.centerX == UIScrollView:0x7680f10.centerX, NSAutoresizingMaskLayoutConstraint:0x76deb70 h=-&- v=-&- UIViewControllerWrapperView:0x76d5b10.width == UINavigationTransitionView:0x76d1ab0.width, NSAutoresizingMaskLayoutConstraint:0x75a3b60 h=--- v=--- V:[UIWindow:0x91979a0(480)], NSAutoresizingMaskLayoutConstraint:0x7595510 h=-&- v=-&- UILayoutContainerView:0x76a5da0.height == UIWindow:0x91979a0.height, NSAutoresizingMaskLayoutConstraint:0x759e020 h=-&- v=-&- UINavigationTransitionView:0x76d1ab0.width == UILayoutContainerView:0x76a5da0.width )

Will attempt to recover by breaking constraint NSLayoutConstraint:0x76d7330 UIView:0x76d3110.centerX == UIScrollView:0x7680f10.centerX

Can you tell me what i'm doing wrong here? Should i remove constraints? Can i do such thing?

Thanks is advance!

Sincerely,

Zoli

1

1 Answers

1
votes

It looks like your custom constraint and the autoresizing mask constraints are clashing. Use this line to avoid that from being generated and add additional constraints as needed.

self.iPhoneMainScrollView.translatesAutoresizingMaskIntoConstraints = NO;