Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS Is my UIView visible on screen?

I have a custom UIView that with a timer displays the current time, which is set inside a UITableViewCell. Is there a way to detect that user is no longer viewing this custom UIView I have (say by navigating to a different tab)? I would like to stop this timer when my UIView is no longer visible on screen. I am using Swift.

I see there is a method I can override called didMoveToWindow, which seem to be triggered when I change tabs, but I'm not very experienced with iOS and what methods or properties to look for to tell if the view is actually visible to the user or not on screen.

I need some kind of method that are called, similar to viewDidAppearand viewDidDisappearfor UIViewController.

Thanks in advance!

like image 884
Bjarte Avatar asked Oct 08 '14 11:10

Bjarte


1 Answers

I found an answer to this that works for this purpose, simply override didMoveToWindow and check if self.window is nil or not:

override func didMoveToWindow() {
    if (self.window == nil) {
        timerStop()
    } else {
        timerStart()
    }
}
like image 83
Bjarte Avatar answered Oct 09 '22 17:10

Bjarte