简体   繁体   English

是否有任何用于UIView的viewDidAppear方法(不是UIViewController)?

[英]Is there any viewDidAppear method for UIView (not UIViewController)?

I need to understand when UIView appears on the screen, so I need an analogue of the viewDidAppear method. 我需要了解UIView何时出现在屏幕上,所以我需要一个viewDidAppear方法的类比。

I found a UIView lifecycle: 我找到了一个UIView生命周期:

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

I tried all of these methods, but I didn't get an answer. 我尝试了所有这些方法,但我没有得到答案。

No there is no viewDidAppear in UIView . UIView没有viewDidAppear you may override func drawRect to do any UI changes that you need on UIView inherited View. 您可以覆盖func drawRect来执行UIView继承的View所需的任何UI更改。

SideNote - In case you want get drawrect to update at later times, Call setNeedsDisplay . SideNote - 如果您希望drawrect更新drawrect ,请调用setNeedsDisplay setNeedsDisplay will not immediately call drawRect but marks the receiver's entire bounds rectangle as needing to be redrawn. setNeedsDisplay不会立即调用drawRect而是将接收者的整个边界矩形标记为需要重绘。

In other words - You should never call drawRect yourself. 换句话说 - 你永远不应该自己调用drawRect。 Instead, you tell the system that drawing needs to be done by using the setNeedsDisplay method, which marks the view as dirty. 相反,您告诉系统需要使用setNeedsDisplay方法完成绘制,该方法将视图标记为脏。 And the drawRect method of the subclass would then be called during the next update cycle. 然后在下一个更新周期中调用子类的drawRect方法。

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 根据OP(@Alexander)的查询,他只需要设置一些变量,因此建议使用以下任何覆盖函数,具体取决于需要执行的操作

  • -(void)didMoveToSuperview - sent immediately after the view is inserted into a view hierarchy. -(void)didMoveToSuperview - 在视图插入视图层次结构后立即发送。

  • -(void)didMoveToWindow - sent immediately after the view gets its window property set. -(void)didMoveToWindow - 在视图获取其窗口属性后立即发送。

  • -(void)willMoveToSuperview:(UIView *)newSuperview - sent immediately before the view is added as a subview to another view; -(void)willMoveToSuperview:(UIView *)newSuperview - 在将视图作为子视图添加到另一个视图之前立即发送; newSuperview may be nil when you remove the view from its superview. newSuperview视图中删除视图时, newSuperview可能为nil。

  • -(void)willMoveToWindow:(UIWindow *)newWindow - sent immediately before the view (or its superview) is added to a window; -(void)willMoveToWindow:(UIWindow *)newWindow - 在视图(或其-(void)willMoveToWindow:(UIWindow *)newWindow视图)被添加到窗口之前立即发送; newWindow may be nil when you remove the view from a window. 从窗口中删除视图时, newWindow可能为nil。

Look, viewDidAppear is method of UIViewController which represents moment when view of ViewController did appear and allows you to do declare what should happen. 看, viewDidAppearUIViewController方法,它表示ViewController view出现的时刻,并允许您声明应该发生什么。

UIView has no method like this. UIView没有这样的方法。 This comes from MVC pattern: controller is in this case UIViewController which controls changes, actions, etc., and view is just what controller shows. 这来自MVC模式:在这种情况下,控制器是UIViewController ,它控制变化,动作等,而view就是控制器显示的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM