NSSplitView and autolayout

后端 未结 9 2020
暗喜
暗喜 2021-02-02 10:55

How should I use auto layout constrains inside NSSplitView subview?

My NSSplitView subview has 3 subview: topPane, tableCont

9条回答
  •  滥情空心
    2021-02-02 11:31

    It took me some time to get my autolayout clean of warnings but I did get it handled in IB (several splitviews and subviews).

    My layout looks like:

    RootView
      |--1st NSSplitView (3 vertical subviews)
          |----UIView (left)
          |----2nd NSSplitView (center & 2 horizontal subviews)
               |---UIView (top)
               |---3rd NSSplitView (bottom & 3 vertical subviews)
                   |---UIView (left)
                   |---UIView (center)
                   |---UIView (right)
          |----UIView (right)

    My problem was, that I had 19 Warnings in all my subviews but my layout looked fine and worked how it should be. After a while I found the cause of my warnings: the constraints of the outer views in my first splitview.

    Both views (left and right) had a width-constraint with "width >= 200" and the center view (2nd splitview) had no constraints (because its min-width and max-width where handled by its subviews).

    The warnings showed me that autolayout wants to shrink my IB-UI-Layout because the calculated min-widths where smaller than my layout but I didn´t want to shrink it in IB.

    I added a fixed constraint "width = 200" to both of the outer subviews of my first splitview and checked "remove at build time".

    Now my layout is free of warnings and everything works how it should be.

    My conclusion:

    I think the problem with autolayout and splitviews is that autolayout can not handle the width-constraints of the subviews. The reason we want to use splitviews is, that we want dynamic width of the views and we want it in both directions, shrink and expans.

    So there is no width <= xxx && width >= xxx . Autolayout can only handle one of it and we get warnings in IB. You can fix this problem with a temporary constraint in IB which will removed before runtime.

    I hope it makes sense what I wrote but it worked fine in my project.

    PS: I could not found any solution until today where I found this thread.. so I guess your posts inspired me :-)

提交回复
热议问题