user6506473
user6506473

Reputation:

Perform action when status bar is clicked

I have seen popular apps like messenger do something with their status bar. When you call someone and go out of the app, there is a red status bar. If you click on it you get back to the app. I have been searching on performing actions when the status bar is clicked. I am not sure if it even involves the status bar. Anyway, if you know how to perform an action, it would be nice to help me out:) I don’t want to seem like to just posting here because i am to lazy. It was actually hard to find information about this.

Upvotes: 0

Views: 831

Answers (2)

Shehata Gamal
Shehata Gamal

Reputation: 100503

First get statusView

extension UIApplication {
    var statusBarView: UIView? {
        return value(forKey: "statusBar") as? UIView
    }
}

Add gesture to it

let tap = UITapGestureRecognizer(target: self, action: #selector(self.handleTap(_:)))

let stV = UIApplication.shared.statusBarView

stV?.addGestureRecognizer(tap)

stV?.isUserInteractionEnabled = true

///

@objc func handleTap(_ sender: UITapGestureRecognizer) {
    print("status clicked")
}

Upvotes: 0

Kunal Shah
Kunal Shah

Reputation: 1121

The status bar is a part of the iOS platform and has really nothing to do with your app. The only thing you can really control is its appearance. The 'different' status bars you see during different instances like when you're on a phone call or when your location is being tracked etc. are all driven by the OS itself.

If you really need to add an action to the status bar, you're better of using a 'fake' transparent view over the status bar and adding a gesture to it, rather than capturing it as one of UIApplication's subviews. This is an example of private API usage. Apple has always been against such practices, and has even rejected apps in certain cases.

Upvotes: 1

Related Questions