UIView did appear?

2019-03-17 13:35发布

I'm wondering, is there a way to get a delegate or something, when a particular UIView has been shown on the screen ?

5条回答
做个烂人
2楼-- · 2019-03-17 14:10

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
    }
}
查看更多
狗以群分
3楼-- · 2019-03-17 14:16

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! =)

查看更多
Anthone
4楼-- · 2019-03-17 14:20

If you manage your logic directly inside the UIView, use:

- didMoveToSuperview

If you manage your logic inside a UIViewController, use :

- viewDidAppear:(BOOL)animated
查看更多
姐就是有狂的资本
5楼-- · 2019-03-17 14:22

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];
}
查看更多
啃猪蹄的小仙女
6楼-- · 2019-03-17 14:30

Try these:

– didAddSubview:
– willRemoveSubview:
– willMoveToSuperview:
– didMoveToSuperview
– willMoveToWindow:
– didMoveToWindow
- viewDidAppear:
查看更多
登录 后发表回答