13

In my appDelegate, I override touchesBegan to detect when the status bar is clicked:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [super touchesBegan:touches withEvent:event];

    CGPoint location = [[[event allTouches] anyObject] locationInView:kWindow];
    CGRect statusBarFrame = [UIApplication sharedApplication].statusBarFrame;

    if (CGRectContainsPoint(statusBarFrame, location)) {
        ///do something 
    }
}

Before iOS 13, this worked; on iOS 13, it does not. The method is never executed. How can I fix this?

Clay Bridges
  • 11,602
  • 10
  • 68
  • 118
dfwang
  • 131
  • 1
  • 4

3 Answers3

3

After some research, I found that the status bar is no longer part of the application on iOS13, it’s part of the system UI (“springboard”). You need to access it via UIStatusBarManager

So this no longer works:

CGRect statusBarFrame = [UIApplication sharedApplication].statusBarFrame;

In iOS 13, you need to get the StatusBar frame like this:

UIStatusBarManager *manager = [UIApplication sharedApplication].keyWindow.windowScene.statusBarManager;
CGRect statusBarFrame = manager.statusBarFrame;
alxlives
  • 5,084
  • 4
  • 28
  • 50
  • hi, alxlives, Thank you for your answer. I tried to use UIStatusBarManager create a view, and add the tap event to this view, but is not responding. – dfwang Sep 03 '19 at 01:51
  • Hello. I don’t really understood the create a view part. Have you tried [this solution](https://stackoverflow.com/a/18953439/3241041) but getting the statusbar frame from UIStatusBarManager ? – alxlives Sep 03 '19 at 12:02
  • @alxlives I think what @dfwang is doing is using the UIStatusBarManager's frame to create a view in the same position, then use that view to intercept touch events? I'm not sure that would work though. The link you posted relies on `touchesBegan` being called but that doesn't get called when the status bar is tapped in iOS 13. So far, I'm not seeing a way to detect taps on the status bar in iOS 13 yet. – Xeaza Sep 06 '19 at 12:43
  • @Xeaza Did you find any solution to detect touch on status bar? – Jayeshkumar Sojitra Sep 25 '19 at 08:11
  • @JayeshkumarSojitra, not yet. I have this open question which includes what I've found out so far: https://stackoverflow.com/questions/57806710/touchesbegan-is-called-in-ios-12-but-is-not-called-in-ios-13 – Xeaza Sep 25 '19 at 12:46
  • This is not an answer to the posted question. – Zia Dec 11 '19 at 17:29
2

In iOS 13, StatusBar Event was managed by UIStatusBarManager, these methods will be called when click the StatusBar:

-[UIStatusBarManager _handleScrollToTopAtXPosition:]
-[UIStatusBarManager handleTapAction:]

So, you can hook -[UIStatusBarManager handleTapAction:] to detecte StatusBar click event

Zhen Tan
  • 21
  • 2
0

If you are looking into the problem of animating a view if the user clicks on the status bar or on the empty dynamic island for quickly scrolling the page to top, then I suggest you using UIScrollViewDelegate's func scrollViewShouldScrollToTop(_ scrollView: UIScrollView) -> Bool method.

Here's the method:

func scrollViewShouldScrollToTop(_ scrollView: UIScrollView) -> Bool {
    // Animation code here
    
    return true
}
Burak
  • 525
  • 4
  • 24
  • Please do not ask for upvotes. It is against SO policy and it's just noise in your question. – HangarRash Jun 21 '23 at 14:19
  • I wonder if it is only your opinion arguing asking for an upvote violates SO policy as I scanned through StackOverflow's 'Public Network Terms of Service', 'Stack Overflow for Teams Terms of Service', 'Privacy Policy', 'General Data Protection Regulation (GDPR)' and many more and did not come across such article. Please refer here such article and I leave my answer without any noise. Otherwise, I prefer so much noise in my answer or in any of my questions as I think these 'noises' leads much more interaction in StackOverflow and I think StackOverflow loves interaction. @HangarRash – Burak Jun 21 '23 at 14:40
  • Please see https://meta.stackoverflow.com/questions/297597/what-should-we-do-with-users-who-ask-for-upvotes-and-to-accept-their-answers and https://meta.stackoverflow.com/questions/271810/is-it-ok-to-ask-to-upvote-accept-my-own-answers among several others you can find on the subject. – HangarRash Jun 21 '23 at 14:46
  • OK, thanks for sharing these two related questions and answers. I checked them and still have not found any clue that the problem you state here is mentioned in any of the policies. Furthemore, the best of such problematic statements mentioned in these is '...If it works, please upvote and mark as accepted.". If you look into what really is my intention with writing a similar but absolutely not the same sentence in my answer by re-reading my relevant sentence carefully, you can easily see that I only ask for a kind upvoting request only so that I know that this answer is useful. – Burak Jun 22 '23 at 13:08
  • So, I do not know if you are a moderator, but it seems you are acting as one and obviously you are so prone to dispensing judgment. Though I see that my upvote request in my answer frustrates you, still I think and believe for this very answer it is appropriate and necessary and I conclude that I am not violating any policies with writing such sentence. Again, thank you for your references and I still prefer you stay aside by not editing my answer unless there is any technical issues with it e.g. the code or its logic. @HangarRash – Burak Jun 22 '23 at 13:23