Layoutsubviews and DrawRect: A discussion on the usage of (cgrect) rect

Source: Internet
Author: User

Layout/positioning related @interface UIView (uiviewhierarchy)-(void) setneedslayout; Note: 1. On the receiver label a tag that needs to be re-laid, layoutsubviews is automatically called in the next cycle of the system runloop. -(void) layoutifneeded; Note: 1. In the name of the method, Uikit will determine if the receiver needs layout. According to Apple's official documentation, the layoutifneeded method should be such that layoutifneeded is not Superview chain, Should be the Subviews chain.    -(void) layoutsubviews; Note: 1. This is the core function, the ultimate goal is to call the function, the developer can not call directly call the function, but may override the function, to add some of their own code.  The function will only make the position, the view size of the number calculation, and will not cause the screen to draw. 2. In Apple's official documentation, it is emphasized that "you should override this method only if the autoresizing behaviors of the subviews don't offer the Behavio   R you want. "  The main idea is that we need to call when we are adjusting the sub-view position inside a class, and if you want to set the Subviews location externally, do not rewrite it. 3. If you want to refresh immediately, first call [view Setneedslayout], set the tag to need layout, then immediately call [view layoutifneeded], implement the layout. Before the first display of the view, the tag always "needs to be refreshed" can be called directly [view layoutifneeded] 4.layoutSubviews Automatic Call Timing 1>init initialization does not trigger Layoutsubviews, 2>ad Dsubview will trigger the layoutsubviews, but frame is 0 o'clock will not trigger the 3> set view frame will trigger the layoutsubviews, of course, if the frame value has changed before and after the settings 4> Changing the size of a uiview will also trigger the Layoutsubviews event on the parent UIView 5> not scrolling will trigger Uiscrollview will trigger Layoutsubviews 6&gtRotating screen triggers the Layoutsubviews event on the parent UIView @end 

Show related @interface UIView (uiviewrendering)-(void) Setneedsdisplay; note: 1. On the receiver label A marker that needs to be redrawn, automatically redraw in the next draw cycle, iphone The device's refresh rate is 60hz, which is 1/60 seconds after redrawing;-(void) Setneedsdisplayinrect: (cgrect) rect; Note: 1. Not only flag is set, but also the area that needs to be refreshed is specified. -(void) DrawRect: (cgrect) rect; Note: 1. This is the core function that eventually causes the display to appear on the screen. Developers can not call the function directly, only rewrite the function, and do something extra to do what we want to do.   is the redraw of receiver, which can get the context. 2. In UIView, rewrite the drawrect: (CGRect) Arect method, you can define the pattern you want to draw yourself. This method is typically only drawn once. This means that the DrawRect method is normally only called once 3. Calling Setneedsdisplay automatically calls DrawRect (note that Apple does not recommend calling the DrawRect method directly), provided the RECT size exists,   If the rect size is not set when UIView is initialized, the DrawRect is not automatically called. The 4.drawRect tune is dropped after the Controller->loadview, Controller->viewdidload two method. So don't worry. In the controller, these view drawrect begin to draw 5. This method is called after calling Sizethatfits, so you can call SizeToFit to calculate the size first.   The system then automatically calls the DrawRect: method. 6. By setting the Contentmode property value to Uiviewcontentmoderedraw.   The drawrect will be called automatically each time the frame is set or changed:. 7. If you use UIView drawing, you can only get the corresponding contextref and draw in the DrawRect: method. 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. 8. If you use Calayer to paintGraph, can only be drawn in Drawincontext: (like fish drawrect), or in the corresponding method in delegate.   The same is called Setneeddisplay, and so on indirectly called the above method. 9. To paint in real time, you cannot use Gesturerecognizer, you can only use Touchbegan and other methods to drop the Setneedsdisplay real-time Refresh screen 10. Call Setneedslayout and layoutifneeded DrawRect is also automatically called, provided that the RECT size exists, and if the rect size is not set at UIView initialization, the DrawRect is not automatically called. @end

@interface UIView (Uiviewgeometry)-(Cgsize) Sizethatfits: (cgsize) size;  Return ' best ' size to fit given size. Does not actually resize view. Default is return existing view size NOTE: 1. The optimal size is calculated but does not change its size (to change the size of the label, but also to assign the result of the calculation to the corresponding view);         2. Parameter size you can enter any value without affecting-(void) SizeToFit;                                  Calls Sizethatfits:with current view bounds and changes bounds size. Note: 1. The optimal size is calculated and changes its size (the view directly changes itself) @en D

Example:

#import "ViewController.h" @interface Viewcontroller () @end @implementation viewcontroller-(void) viewdidload {[Super V    Iewdidload];    UILabel * Label =[[uilabel alloc]initwithframe:cgrectmake (100, 100, 100, 20)];    Label.backgroundcolor =[uicolor Bluecolor];    Label.text [email protected] "sizethatfits: and SizeToFit Verification";    Label.font=[uifont SYSTEMFONTOFSIZE:20];    Label.textcolor =[uicolor Blackcolor];            Nsinteger I =1;//1 or 2 switch (i) {Case 1: [Self sizethatfitssender:label];        Break        Case 2: [Self sizetofitsender:label];    Default:break; } [Self.view Addsubview:label];} Sizethatfits: Usage Verification-(void) Sizethatfitssender: (UILabel *) label{[Label Sizethatfits:cgsizemake (20, 20)];//    It calculates the optimal size but does not change its size.    NSLog (@ "label sizethatfits frame=%@", Nsstringfromcgrect (Label.frame)); NSLog (@ "Best size =%@", nsstringfromcgsize ([Label Sizethatfits:cgsizemake (2, 1)]);} Validation of SizeToFit Usage-(void) SizetofitsendeR: (UILabel *) label{[Label sizetofit];//calculates the optimal size and changes its size NSLog (@ "TestLabel sizetofit frame =%@", Nsstri Ngfromcgrect (Label.frame));}
Sizethatfits: Validation of usage

Print results




Validation of the use of SizeToFit

Print results


















Layoutsubviews and DrawRect: A discussion on the usage of (cgrect) rect

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.