I need to understand when UIView appears on the screen, so I need an analogue of the viewDidAppear method.
I found a UIView lifecycle:
I tried all of these methods, but I didn't get an answer.
The viewWillAppear method is called before loading the actual view. The viewDidAppear method is called when the view is already loaded, and you want to show something.
The difference between viewDidAppear and viewDidLoad is that viewDidAppear is called every time you land on the screen while viewDidLoad is only called once which is when the app loads.
viewDidAppear is called once you see the loaded view on screen. It is called after view appeared. ViewDidAppear is called everytime when you see the view after it is loaded. if you push and then pop any other viewController on that view then again viewDidAppear gets called.
viewWillAppear(_:)Always called after viewDidLoad (for obvious reasons, if you think about it), and just before the view appears on the screen to the user, viewWillAppear is called.
No there is no viewDidAppear
in UIView
. you may override func drawRect
to do any UI changes that you need on UIView
inherited View.
SideNote - In case you want get drawrect
to update at later times, Call setNeedsDisplay
. setNeedsDisplay
will not immediately call drawRect
but marks the receiver’s entire bounds rectangle as needing to be redrawn.
In other words - You should never call drawRect yourself. Instead, you tell the system that drawing needs to be done by using the setNeedsDisplay method, which marks the view as dirty. And the drawRect method of the subclass would then be called during the next update cycle.
As per the queries from OP(@Alexander), he just need to set some variable so it advisable to use any of the following override functions, depending on action need to be performed
-(void)didMoveToSuperview
- sent immediately after the view is
inserted into a view hierarchy.
-(void)didMoveToWindow
- sent immediately after the view gets its
window property set.
-(void)willMoveToSuperview:(UIView *)newSuperview
- sent
immediately before the view is added as a subview to another view;
newSuperview
may be nil when you remove the view from its
superview.
-(void)willMoveToWindow:(UIWindow *)newWindow
- sent immediately
before the view (or its superview) is added to a window; newWindow
may be nil when you remove the view from a window.
Look, viewDidAppear
is method of UIViewController
which represents moment when view
of ViewController did appear and allows you to do declare what should happen.
UIView
has no method like this. This comes from MVC pattern: controller is in this case UIViewController
which controls changes, actions, etc., and view
is just what controller shows.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With