Improve this question. Add a comment. Active Oldest Votes. Just call [self. Improve this answer. Sam Ritchie Sam Ritchie It doesn't appear to be working! Is that line of code you added getting called? Check with NSLog, or with a breakpoint. Time Machine has a good point -- [self. In the nib file, make sure that when you click the MyView instance and go to the connections inspector Command-2 , you see a referencing outlet to File's Owner.
If not, click that little circle and drag the line over to File's Owner. When you release, select the right property. No worries Show 5 more comments. Sign up or log in Sign up using Google. Instead, it gets called by the system whenever drawing is required, which allows it to to avoid multiple redraws if drawRect is called several times in a row.
Instead, if you want a view to redraw immediately, you should call its setNeedsDisplay method like this:. That will ask UIKit to redraw the button using drawRect , but only if a redraw is not already queued.
Use the Xcode extension to open recent projects, search iOS documentation and clear derived data. Or, build your own extension with an easy-to-use API.
The method in drawRect is called after re-drawing again, it is a complete waste of the system Resources, and through setNeedsDisplay, ios will be very smart to determine that it does not need to call the drawRect method, so as to avoid repeated calls to resources! Similarly, setNeedsLayout also uses the same mechanism to avoid the reuse of resources! From this we can infer the subtleties of this design, ios by many exquisite designs to alleviate the current situation of insufficient mobile phone resources, such as cell reuse and so on.
Init initialization will not trigger layoutSubviews but when it is initialized with initWithFrame, when the value of rect is not CGRectZero, it will also trigger 2, addSubview will trigger layoutSubviews 3.
The frame of the view will trigger layoutSubviews, of course, the premise is that the value of the frame has changed before and after 4, scrolling a UIScrollView will trigger layoutSubviews 5, rotating the screen will trigger the layoutSubviews event on the parent UIView 6, changing the size of a UIView will also trigger the parent The layoutSubviews event on UIView is emphasized in Apple's official documentation: You should override this method only if the autoresizing behaviors of the subviews do not offer the behavior you want.
The reverse meaning means: if you want to set the position of subviews externally, don't rewrite. Refresh sub-object layout -layoutSubviews method: This method is the default does not do anything, you need to subclass rewrite -setNeedsLayout method: marked as needing to re-layout, asynchronous calls layoutIfNeeded refreshed layout, does not refresh immediately, but will be called layoutSubviews - layoutIfNeeded method: If there is a mark that needs to be refreshed, immediately call layoutSubviews for layout if there is no mark, layoutSubviews will not be called.
If you want to refresh immediately, you must first call [view setNeedsLayout], set the mark to need layout, and then immediately call [ view layoutIfNeeded], to achieve the layout before the view is displayed for the first time, the mark is always "need to refresh", you can directly call [view layoutIfNeeded] redraw- drawRect: CGRect rect method: override this method, perform the redraw task -setNeedsDisplay method: marked as needing to redraw, call drawRect asynchronously -setNeedsDisplayInRect: CGRect invalidRect method: marked as requiring local redraw sizeToFit will automatically call sizeThatFits method; sizeToFit should not be rewritten in subclasses, sizeThatFits should be rewritten The parameter passed in sizeThatFits is the current size of the receiver, and returns a suitable size sizeToFit can be directly called manually.
0コメント