List the relevant methods for iOS layout:
Layoutsubviews
layoutifneeded
Setneedslayout
Setneedsdisplay
DrawRect
Sizethatfits
SizeToFit
Probably often used in the above several, in detail there should be something else.
Layoutsubviews
This method, by default, does not do whatever it takes to rewrite the subclass. The system will call this method at very many times:
1. Initialization does not trigger layoutsubviews, but it is triggered when a frame that is not Cgrectzero is set.
2.addSubview will trigger Layoutsubviews
3. Setting the frame of the view will trigger layoutsubviews. Of course, the premise is that the frame value has changed before and after setting
4. Scrolling a uiscrollview will trigger layoutsubviews
5. Rotate screen to trigger the Layoutsubviews event on the parent UIView
6. Changing the size of a uiview also triggers the Layoutsubviews event on the parent UIView
In Apple's official documentation, it is emphasized that you should override this method only if the autoresizing behaviors of the subviews does not offer the behavior You want.layoutsubviews, we need to call when we adjust the sub-view position inside a class. In turn, it means: Suppose you want to set the subviews position externally. Don't rewrite it.
Setneedslayout
Marked as needing another layout. Not immediately refreshed, but layoutsubviews must be called
Update immediately with layoutifneeded
layoutifneeded
Assuming that there is a need to refresh the tag, call Layoutsubviews immediately to layout
This animation is practical to lift a chestnut.
With a label on it with a button in the middle. Label has been actively layout to the upper left corner.
And then our left constraint.
@IBOutlet weak var leftcontrain:nslayoutconstraint!
Declared well in the viewdidload. Then connect in the Main.storyboard. When we click on the button, we change the distance from the left to 100.
Add this to the button's Click event.
leftcontrain.constant = 100
Then we want an animated effect.
Let's say this.
Uiview.animatewithduration (0.8, delay:0, usingspringwithdamping:0.5, initialspringvelocity:0.5, Options: Uiviewanimationoptions.allowanimatedcontent, animations: {
self.leftContrain.constant = 100
}, Completion:nil)
You'll find the egg. In fact, this sentence self.leftContrain.constant = 100 is just running the setneedslayout tag that needs to be laid out again. But it didn't run right away. So we need to call this method in the animation layoutifneeded
So the code should be written like this.
leftcontrain.constant = 100
Uiview.animatewithduration (0.8, delay:0, usingspringwithdamping:0.5, initialspringvelocity:0.5, Options: Uiviewanimationoptions.allowanimatedcontent, animations: {
Self.view.layoutIfNeeded ()//immediate realization of layout
}, Completion:nil)
So the above no matter how many constraints change. Just need to use one time self.view.layoutIfNeeded () in the animation, all of the way will have been animated. Suppose some changes do not want to animate. Run self.view.layoutIfNeeded () before the animation
DrawRect
This method is used for redrawing.
The drawrect will be called in the following cases:
1, assuming that the UIView initialization is not set rect size, will directly lead to drawrect not be actively invoked by themselves.
The DrawRect call is dropped after the Controller->loadview, Controller->viewdidload two methods. So don't worry about it. In the controller, The drawrect of the view began to draw. This allows you to set some values in the controller to the view (assuming that some variable values are required for these view draw).
2, the method is called after calling SizeToFit. So it is possible to call SizeToFit first to calculate the size. The system then voluntarily calls the DrawRect: method. 3, by setting the Contentmode property value to Uiviewcontentmoderedraw. Then you will be actively calling DrawRect each time you set or change the frame:. 4, directly call Setneedsdisplay, or setneedsdisplayinrect: Trigger DrawRect:, but there is a precondition that rect cannot be 0. Above 1, 2 recommended. And 3,4 does not advocate
DrawRect methods use note points:
1, if use UIView drawing. can only get the corresponding contextref in the DrawRect: Method and draw.
Suppose that in other methods the fetch is obtained to a invalidate ref and cannot be used for drawing. DrawRect: The method cannot display the call manually. You must call Setneedsdisplay or Setneedsdisplayinrect to make the system self-tuning the method.
2, if using Calayer drawing, can only be drawn in Drawincontext: (similar to DrawRect). or the corresponding method is drawn in the delegate. The same is called Setneeddisplay and other indirect calls above Method 3, to real-time paint, can not use the Gesturerecognizer, only to use Touchbegan and other methods to drop setneedsdisplay real-time refresh screen
SizeToFit
SizeToFit will call the Sizethatfits method on its own initiative;
SizeToFit should not be overridden in subclasses and should be rewritten sizethatfits
The sizethatfits passed in is the current size of receiver and returns a suitable size
SizeToFit can be manually called directly SizeToFit and Sizethatfits methods are not recursive. is not responsible for subviews. Just take care of yourself
Paper/Big stone cloth (Jane book author)
Original link: http://www.jianshu.com/p/eb2c4bb4e3f1
Ios-setneedslayout and other layout methods