How do I detect touches on UIStatusBar/iPhone

你离开我真会死。 提交于 2019-12-02 04:23:25

问题


I'm trying to detect any touch on the iPhone's UIStatusBar but its not working. I've tried subclassing UIApplication and UIWindow to access it but still nothing.


回答1:


Based on Tom's answer, I wrote the code. It works well.

- (void)viewDidLoad {
    [super viewDidLoad];
    // required
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectZero];
    scrollView.delegate = self;
    scrollView.contentSize = CGSizeMake(0.0f,1.0f);
    [scrollView setContentOffset:CGPointMake(0.0f,1.0f) animated:NO];
    // optional
    scrollView.scrollsToTop = YES; // default is YES.
    [self.view addSubview:scrollView];
}

- (BOOL)scrollViewShouldScrollToTop:(UIScrollView *)scrollView {
    NSLog(@"Detect status bar is touched.");
    /* Your own code. */
    return NO;
}



回答2:


You need to have a UIScrollView in your responder chain, and it needs to have a delegate set. You can then override scrollViewShouldScrollToTop: in the delegate, which will be called when the user taps on the status bar. Be sure to return NO if you don't want the default behavior of scrolling the scroll view back to the top.




回答3:


You can't. The status bar is held by the application. You can only turn it on and off.

More importantly, any fiddling with the status bar is likely to get your app rejected by Apple because it could interfere with the operation of the hardware e.g. disguising or hiding a low battery.

If you need full screen touches for your views you should just hide the status bar.



来源:https://stackoverflow.com/questions/2470562/how-do-i-detect-touches-on-uistatusbar-iphone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!