美文网首页
包含多个scrollView时, 点击状态栏滚回顶部

包含多个scrollView时, 点击状态栏滚回顶部

作者: NateLam | 来源:发表于2016-11-29 10:19 被阅读13次

先在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];
}

相关文章

网友评论

      本文标题:包含多个scrollView时, 点击状态栏滚回顶部

      本文链接:https://www.haomeiwen.com/subject/ineapttx.html