How to resize a parent view based on the size of subviews layouted with Autolayout

前端 未结 2 565
礼貌的吻别
礼貌的吻别 2020-12-13 04:53

I am preparing a custom view which looks similar to the example shown in the screenshot. I add all user interface components (labels, text fields, radio buttons, ..) at runt

相关标签:
2条回答
  • 2020-12-13 05:32

    If you are use NSStackView then make sure you don't set a constraint for it's height and width and make sure all sides are anchored.

    Also make sure that you set the content Hugging property to 1000.

    0 讨论(0)
  • 2020-12-13 05:48

    I've done this with the following setup:

    • the container view has no height constraint
    • the top subview has a spacing constraint from the top of the container view
    • the contained views all have spacing constraints between them
    • the bottom view has a spacing constraint to the bottom of the container view

    The auto layout system satisfies these constraints by resizing the container to fit its contents.

    Of course, you need to dynamically set the constraints as you alter the contents.

    0 讨论(0)
提交回复
热议问题