在做PagerTab https://github.com/ming1016/PagerTab 这个滑动切换多页面的控件时发现如果有多个scroll view系统的点击statusbar滚动回顶部功能就会失效,查了些资料找到了解决的办法,现记录下。
主要思路是先创建一个UIWindow对象给它添加一个tap手势
static UIWindow *topWinow; + (void)initialize { topWinow = [[UIWindow alloc] init]; topWinow.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 20); topWinow.windowLevel = UIWindowLevelAlert; [topWinow addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(winTap)]]; }
递归搜索所有view查找当前位置合适的scroll view使其能够滚动到顶部
+ (void)seachScrollViewInView:(UIView *)view { for (UIScrollView *subView in view.subviews) { if ([subView isKindOfClass:[UIScrollView class]] && [self isCurrentShowView:subView]) { //开始进行滚动 CGPoint offset = subView.contentOffset; offset.y = -subView.contentInset.top; [subView setContentOffset:offset animated:YES]; } //寻找子视图的子视图 [self seachScrollViewInView:subView]; } } //根据位置判断是否合适 + (BOOL)isCurrentShowView:(UIView *)view { UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow; CGRect currentFrame = [keyWindow convertRect:view.frame fromView:view.superview]; CGRect winBounds = keyWindow.bounds; BOOL intersects = CGRectIntersectsRect(currentFrame, winBounds); return !view.isHidden && view.alpha > 0.01 && view.window == keyWindow && intersects; }
这部分的修改已经push到github上完整的实现代码可以在PagerTab的Helper/SMTopWindow.m里看到。