The difference between ayoutsubviews layoutifneeded setneedslayout

Source: Internet
Author: User
Layoutsubviews layoutifneeded setneedslayout setneedsdisplay drawrect sizethatfits

SizeToFit
layoutsubviews

This method, by default, does nothing and requires subclasses to rewrite it. The system will call this method at many times:

1. Initialization does not trigger Layoutsubviews, but 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, if the frame value has changed before and after the 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, this means: If you want to set the Subviews location externally, do not rewrite it. Setneedslayout

Marked for need of re-layout, not immediately refreshed, but layoutsubviews must be called
Update layoutifneeded with layoutifneeded now

If there are tags that need to be refreshed, call layoutsubviews immediately for layout

This animation is useful for lifting a chestnut.


As shown above, there is a label with a button in the middle, and the label has been automatically laid out to the upper left corner. And then our left constraint.

  @IBOutlet weak var leftcontrain:nslayoutconstraint!

Declare it in Viewdidload, and then connect in the Main.storyboard. When we click the button, we change the distance from the left to 100.

Add this sentence to the button's Click event.

leftcontrain.constant = 100

Then we want an animated effect.
If you do this,

   Uiview.animatewithduration (0.8, delay:0, usingspringwithdamping:0.5, initialspringvelocity:0.5, Options: Uiviewanimationoptions.allowanimatedcontent, animations: {
                self.leftContrain.constant = +
            }, Completion: Nil

You'll find the egg. In fact, this sentence self.leftContrain.constant = 100 is only performed the setneedslayout marked the need to re-layout, but not immediately executed. So we need to call this method in the animation layoutifneeded
So the code should be written like this.

Leftcontrain.constant =
        uiview.animatewithduration (0.8, delay:0, usingspringwithdamping:0.5, initialspringvelocity:0.5, options:UIViewAnimationOptions.AllowAnimatedContent, animations: {
                Self.view.layoutIfNeeded ()//Implement layout now
            }, Completion:nil)

So the above no matter how many constraint changes, only need to use in the animation once self.view.layoutIfNeeded (), all will have animated way. If some changes do not want to animate. Execute self.view.layoutIfNeeded () DrawRect before the animation

This method is used for redrawing.

The drawrect is called in the following cases:
1. If the rect size is not set at UIView initialization, the drawrect will not be automatically called. The DrawRect call is dropped after the Controller->loadview, Controller->viewdidload two method. So don't worry. In the controller, The drawrect of the view began to draw. This allows you to set some values in the controller to the view (if some variable values are needed for these view draw).
2, this method is called after calling SizeToFit, so you can call SizeToFit to calculate the size first. The system then automatically calls the DrawRect: method. 3, by setting the Contentmode property value to Uiviewcontentmoderedraw. The drawrect will be called automatically each time the frame is set or changed:. 4. Call Setneedsdisplay directly, or setneedsdisplayinrect: Trigger DrawRect:, but there is a precondition that rect cannot be 0. Above 1, 2 recommended; 3,4 does not advocate

DrawRect methods use note points:
1, if using UIView drawing, only in the DrawRect: method to obtain the corresponding Contextref and drawing. If obtained in other methods, it gets to a invalidate ref and cannot be used for paint. DrawRect: The method cannot manually display the call, and the system must be automatically tuned by calling Setneedsdisplay or Setneedsdisplayinrect. 2, if using Calayer drawing, can only be drawn in Drawincontext: (similar to DrawRect), or in the corresponding method of delegate. Also call Setneeddisplay and other indirect calls above Method 3, to real-time paint, can not use Gesturerecognizer, can only use Touchbegan and other methods to drop the Setneedsdisplay real-time refresh screen SizeToFit SizeToFit automatically calls the Sizethatfits method; SizeToFit should not be overridden in subclasses and should be rewritten sizethatfits Sizethatfits incoming parameters are receiver's current size, return 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

Original link: http://www.jianshu.com/p/eb2c4bb4e3f1
Copyright belongs to the author, please contact the author to obtain authorization, and Mark "book author".

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.