OS X Cocoa Auto Layout hidden elements

后端 未结 8 1801
被撕碎了的回忆
被撕碎了的回忆 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:55

    I found a seemlingly decent way to do this as well. It's similar to David's. Here's how it works in code. I created the superview and all it's subviews, even the ones that may not always be showing. I added many of the constraints such as V:|-[_btn] to the superview. As you can see at the end of those constraints there is no link to the bottom on the superview. I then created two arrays of constraints for both states of the view, for me the difference is a 'More Options' disclosure triangle. Then when the triangle is clicked depending on it's state I add and remove constraints and subviews accordingly. For example to add I do:

    [self.backgroundView removeConstraints:self.lessOptionsConstraints];
    [self.backgroundView addSubview:self.nameField];
    [self.backgroundView addConstraints:self.moreOptionsConstraints];
    

    The constraints I removed tied the button to the bottom of the superview like V:[_btn]-|. The constraints I added look like V:[_btn]-[_nameField]-| as you can see this constraint places the new view in between the original view above it and the bottom of the superview which extends the superview's height.

提交回复
热议问题