问题
Looking for an auto layout solution where two buttons lie side-by-side when there is enough space to do so, but otherwise (when the view is displayed with limited width) rest on top of one another.
I don't even know where to begin to think this through with auto layout. Any help is appreciated!
回答1:
I'd imagine this being a combo between autolayout and an NSLayoutConstraint property constant, whereby you set the constraints in IB/Storyboard, and click-drag the trailing space to container constraint for each uibutton onto a viewcontroller as some named IBOutlet property. (i.e. myconstraint1, myconstraint2.)
Then, upon some condition where you can determine the limited width (rotation event, adding a new view?), you set the two constraint constants to the same value, e.g. self.myconstraint1.constant = self.myconstraint2.constant = someValue.
Not knowing the code, I can't tell you the math of course, nor would I know what to reset things to, but otherwise, programmatically setting the nslayoucontraint.constant should give you the desired effect of two uibutton views overlapping one another horizontally.
Similarly, to move them so the Y coordinates differ, you use a similar approach with the vertical spacing to bottom layout constraint for one or both of the buttons.
来源:https://stackoverflow.com/questions/24439421/ios-auto-layout-two-buttons-side-by-side-when-theres-enough-space-otherwise