iOS 6.0 AutoLayout issue with constraints: subviews disappear when rotated

人走茶凉 提交于 2019-12-11 04:28:19

问题


Hi I have designed a XIB using AutoLayout constriants to display two rows of buttons, with three buttons in each row, as shown in the screenshot (I'll place the buttons in the Red UIViews after I get a satisfactory layout):

So far so good, in portrait mode as shown above the buttons look fine. However when I rotate to landscape the header (in blue) and parent view (in grey) resize and appear correctly, but the red button views just disappear (see below).

What is the best way to set the contraints to get this to work right? I have followed the Ray Wenderlich tutorial and used the same Editor\Pin]Vertical Spacing and Editor\Pin\Horizontal spacing that he uses in his example: Ray Wenderlich tutorial

I also get a lot of constraint errors such as the follows:

        Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 
(
    "<NSLayoutConstraint:0x859abe0 UIView:0x85a8fb0.height == UIView:0x85a8e60.height>",
    "<NSLayoutConstraint:0x859ab60 V:[UIView:0x85a8fb0]-(114)-|   (Names: '|':UIView:0x85a8170 )>",
    "<NSLayoutConstraint:0x859ab20 V:|-(424)-[UIView:0x85a8fb0]   (Names: '|':UIView:0x85a8170 )>",
    "<NSLayoutConstraint:0x85a3430 V:|-(150)-[UIView:0x85a8170]   (Names: '|':UIView:0x85a8da0 )>",
    "<NSLayoutConstraint:0x85a33b0 V:[UIView:0x85a8170]-(NSSpace(20))-|   (Names: '|':UIView:0x85a8da0 )>",
    "<NSAutoresizingMaskLayoutConstraint:0x75ac610 h=--& v=--& V:[UIView:0x85a8da0(704)]>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x859ab60 V:[UIView:0x85a8fb0]-(114)-|   (Names: '|':UIView:0x85a8170 )>

Break on objc_exception_throw to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.

I'm at my wits end with this, so any help from this erudite group would be most appreciated...!


回答1:


The error means you have conflicting constraints. Try to get it working for one row.

  1. Pin each red box with a vertical spacing to the blue header box
  2. Pin the left red box with a leading space to superview
  3. Pin the right red box with a trailing space to superview
  4. Set horizontal spacing to the left and right of the center red box
  5. Fix the height
  6. Select all three red boxes and set them to have equal widths (widths equally)
  7. Remove any extraneous constraints added by IB
  8. Test on device. (I have simulating landscape/portrait) in interface builder sometimes changes constraints. 9 It should look like the attached image



来源:https://stackoverflow.com/questions/14989254/ios-6-0-autolayout-issue-with-constraints-subviews-disappear-when-rotated

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!