先在pch写一个宏
#define kStatusBarTappedNotification @"statusBarTappedNotification"
在AppDelegate.m
//解决多个scrollView并存时无法自动滚回顶部
#pragma mark - Status bar touch tracking
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesBegan:touches withEvent:event];
CGPoint location = [[[event allTouches] anyObject] locationInView:[self window]];
CGRect statusBarFrame = [UIApplication sharedApplication].statusBarFrame;
if (CGRectContainsPoint(statusBarFrame, location)) {
[self statusBarTouchedAction];
}
}
- (void)statusBarTouchedAction {
[[NSNotificationCenter defaultCenter] postNotificationName:kStatusBarTappedNotification object:nil];
}
在初始化需要滚动的scrollView的那个类中, 注册通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(statusBarTappedAction:) name:kStatusBarTappedNotification object:nil];
通知响应事件
#pragma mark - 点击状态栏的通知响应事件
- (void)statusBarTappedAction:(NSNotification*)notification {
NSLog(@"StatusBar tapped");
CGPoint offset = _collectionView.contentOffset;
offset.y = - _collectionView.contentInset.top;
[_collectionView setContentOffset:offset animated:YES];
}