OS X Cocoa Auto Layout hidden elements

后端 未结 8 1723
被撕碎了的回忆
被撕碎了的回忆 2021-01-31 17:06

I am trying to use the new Auto Layout in Lion because it seems quite nice. But I can not find good information about how to do things. For example:

I have two labels:

8条回答
  •  日久生厌
    2021-01-31 17:46

    I don't think you could do it that way. If you made the layout for label 2 be based on a distance constraint from label 1, even if you made label 1 auto-collapse to zero height when it has no content, label 2 is still going to be that distance down, ie in:

    +----------------+
    | +------------+ |
    | + label 1    | |
    | +------------+ |
    |        ^       |
    |        ^       !
    | +------------+ |
    | | label 2    | |
    | +------------+ |
    +----------------+
    

    Where ^ is the autolayout distance constraint - If Label 1 knows how to become zero height when it's string is empty, you're still going to get:

    +----------------+
    | +------------+ |
    |        ^       |
    |        ^       !
    | +------------+ |
    | | label 2    | |
    | +------------+ |
    +----------------+
    

    Maybe it is possible by creating your NSLayoutConstraint manually. You could make the second attribute be the height of label 1, make the constant zero, and then carefully work out what the multiplier would be to make the distance be what you want based on a multiple of the non-zero label height.

    But having done all this, you've now coded an NSLabel subclass that auto-sizes, created a constraint object manually instead of via the visual language, and bent NSLayoutConstraint beyond its will.

    I think you're better off just changing the frame of label 2 if label 1's string is blank!

提交回复
热议问题