iOS: Auto Layout: Two buttons side-by-side when there's enough space, otherwise on top of one another

走远了吗. 提交于 2020-01-05 11:05:39

问题


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

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