Is there any viewDidAppear method for UIView (not UIViewController)?
Asked Answered
L

2

7

I need to understand when UIView appears on the screen, so I need an analogue of the viewDidAppear method.

I found a UIView lifecycle:

  1. willMoveToSuperview
  2. invalidateIntrinsicContentSize
  3. didMoveToSuperview
  4. awakeFromNib
  5. willMoveToWindow
  6. needsUpdateConstraints
  7. didMoveToWindow
  8. setNeedsLayout
  9. updateConstraints
  10. layoutSubviews
  11. drawRect

I tried all of these methods, but I didn't get an answer.

Lobscouse answered 17/12, 2018 at 6:6 Comment(5)
no there is not but you can use custom delegates to call the viewDidApppear present on your ViewControllerEditorial
no.. there is no any such method..Horseweed
Hi Alexander, viewDidAppear is the method in UIViewController. We could have find proper solution, if you share what you are trying to achieve?Chinn
@LalKrishna, thanks. I need a variable that shows that UIView has appeared.Lobscouse
@AlexanderGalkin - I have added more relevant tags to this question. Please approve the edit. Thanks.Howell
H
13

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. setNeedsDisplaywill 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.

Howell answered 17/12, 2018 at 6:15 Comment(3)
thanks! I need a variable that indicates that a UIView has appeared. If I override the func drawRect and set the variable to true, will it be correct?Lobscouse
Though this will work but just to set a variable calling drawRect is not a good idea. You may use 'willMoveToSuperview' or 'didMoveToSuperview' for this purpose. What is the purpose of this variable ?Howell
This link have a nice description of when each of the methods gets called. You may use it which suits your purpose. https://mcmap.net/q/239955/-uiview-how-to-get-notified-when-the-view-is-loadedHowell
M
1

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.

Masse answered 17/12, 2018 at 6:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.