I'm wondering, is there a way to get a delegate or something, when a particular UIView
has been shown on the screen ?
Asked
Active
Viewed 4.1k times
54

BinaryMisfit
- 29,219
- 2
- 37
- 44

nosuic
- 1,350
- 3
- 18
- 23
5 Answers
44
Swift version. Inside your UIView class just:
override func willMove(toWindow newWindow: UIWindow?) {
super.willMove(toWindow: newWindow)
if newWindow == nil {
// UIView disappear
} else {
// UIView appear
}
}

buxik
- 2,583
- 24
- 31
-
Note that when view will disappear from screen this might get called 3 times (first with nil, then with window and then again with nil) which might not be what you expected, see: https://stackoverflow.com/questions/34484518/willmovetowindow-is-called-twice – Leszek Szary Jun 29 '20 at 08:07
29
Try these:
– didAddSubview:
– willRemoveSubview:
– willMoveToSuperview:
– didMoveToSuperview
– willMoveToWindow:
– didMoveToWindow
- viewDidAppear:

Max
- 16,679
- 4
- 44
- 57
-
11None of these appear to be called after autolayout so you can't adjust the view's children and have it keep. – devios1 Oct 28 '13 at 17:36
26
If you manage your logic directly inside the UIView, use:
- didMoveToSuperview
If you manage your logic inside a UIViewController, use :
- viewDidAppear:(BOOL)animated

David Ansermot
- 6,052
- 8
- 47
- 82
8
If you are managing the UIView
via a UIViewController
, then you can use the -viewDidAppear:
method:
- (void) viewDidAppear:(BOOL) animated {
//do stuff...
[super viewDidAppear:animated];
}

Jacob Relkin
- 161,348
- 33
- 346
- 320
-3
Another way to find out when a control is on screen is to subclass the View or Control and override drawRect
...
However, it's called when it's drawn and not only when first shown. So it's only sometimes what you want. It worked for my case. Make sure to call super as well! =)

David Ansermot
- 6,052
- 8
- 47
- 82

Ben
- 843
- 8
- 9