Can a UIView know when it is on the screen

I have a UIView with a large number of components enclosed in it, and would like to update some of them if the view is deleted or if its parent view controller popped up / clicked. Is it possible for UIView to get this information

Similar to the method in UIViewController

 -(void)viewWillAppear; 

I would like something like

 -(void)UIViewWillAppear; 

Edit for some comments that I saw:

I will explain a little more

But I have a special case when the UIView had to add a β€œfloating view” on top of itself (imagine scaling / panning / scrolling a subclass of UISCrollView with a floater on top of it), so that when I scroll the floating view, it remains in place relative to the supervision. I tried to recount the new origin of the "floater" inside the method -(void)layoutSubviews , but the permutation was very volatile. To solve this variability problem, a custom UIView added a floating view (which is theoretically a preview of it) as a subquery for its supervisor (a bit of violinist language :)). Now a problem arises when the user-defined UIView is deleted (or its containing view controller is pushed out of the screen). As a special UISCrollView removes the floating view from its supervisor.

+4
source share
3 answers

You can override willMoveToSuperview: to find out when the view is inserted into the hierarchy and when it was deleted. This is probably not what you want, as the view may be part of the hierarchy and not be inserted by itself.

To find out if this uses willMoveToWindow: on the screen. If the argument is not zero, the view simply became part of the visible hierarchy of views.

If you need to do something after the change, use didMoveToWindow:

+2
source

UIView does not appear / do not disappear "by accident" or when they want - your view controllers (or code) control this. Therefore, you should find out when you show them, and the call code that you need.

0
source

The reference to the UIView class has some change in the observed kvo.

By implementing -(void)willRemoveSubview:(UIView *)subview , you can see the opposite.

UPDATE After reading the explanation:

I hope I understood correctly. I did something similar, but with a UITableView, not a UIScrollView (but they are exactly the same).

It was like a pop-up detailed view. I decided how you already did this by adding a detailed view to the UITableView supervisor, and then I added a UIButton closure in the detailed view with the corresponding IBOutlet:

 @interface CustomUIView : UIView @property(nonatomic,weak) IBOutlet UIButtonView *closingButton; -(void)closeDetail:(IBAction)action; @end 

and the action was simple:

 -(void)closeDetail:(IBAction)action { // do your cleaning/change to the detail subviews [self removeFromSuperview]; // <-- clsoe the detail view } 

sdsds

0
source

Source: https://habr.com/ru/post/1500137/


All Articles