Remove Auto-Layout Constraints for Specific Object

前端 未结 6 414
走了就别回头了
走了就别回头了 2020-12-24 05:16

I have a UIImageView embedded in a UIView. My entire app uses AutoLayout, but I want to remove constraints for the

6条回答
  •  醉梦人生
    2020-12-24 05:34

    There are two distinct issues here:

    1. How to use Xcode to setup a UIImageView without constraints
    2. How to allow the UIImageView to be panned and/or resized when its layout is handled by constraints.

    Regarding (1), jturton is correct. As long as you have enabled auto layout within Xcode, Xcode will guarantee/require that there are sufficient constraints to uniquely determine the size and location of your view.

    If you have not manually specified enough constraints in Xcode (via the "user constraints" that appear in blue in the layout inspector), then Xcode will add new constraints that it guesses (which appear in purple) until there are sufficient constraints to determine the layout.

    If the UIImageView had no constraints on it, its layout would not be determined, so you cannot set that up in Xcode. To fix this, what you should do is use Xcode to setup IBOutlets to all the constraints you want to remove, and then in your UIView's awakeFromNib: or in your UIViewController's viewDidLoad: you manually remove the constraints by calling removeConstraints:.

    However, now your UIImageView's layout is underdetermined. So at this point you will want to manually add new constraints that will allow you to move and resize the UIImageView, which brings us to question (2).

    Regarding (2), how to resize & move a view whose layout is determined by constraints, the answer is to setup gesture recognizers as usual to detect the pinch & pan gestures, but instead of those gesture recognizers calling setFrame: to modify the view, they should modify the constant parameter of a NSLayoutConstraint that determines the frame of the UIImageView and then call layoutIfNeeded:, which will cause the layout system to immediately translate that modified constraint into a new frame.

    So, for instance, suppose you want to manipulate the UIImageView with a set of layout constraints that were very similar to familiar setFrame: calls. In that case, after removing the constraints setup by Xcode, you could add constraints to the view that specify its width, its height, and its space from the top and left of the superview. Then the action handler for your pan gesture recognizer, would just update the constant parameter of the pacer gesture recognizer, and your pinch gesture recognizer could just update the constant parameter of the height and width constraints.

    Another way to do (2), which is the same under the hood but might be easier in practice, is to set translatesAutoresizingMaskIntoConstraints=YES. This will do two things. It will cause the auto layout system to automatically generate constraints in the view's superview based on the view's autoresizingMask. Second – crucially! – it will cause the layout system to automatically translate calls to setFrame: into modifications of those constraints.

提交回复
热议问题