从iOS10开始,苹果官方添加点击状态栏,让UIScrollView回滚到顶部,但是iOS9之前的版本需要程序员写代码实现该功能。
实现思路
1,状态栏处添加一个UIWindow
创建一个UIWindow对象,并添加点击手势,而且windowLevel大于UIWindowLevelStatusBar
+ (void)initialize
{
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.25 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
window_ = [[UIWindow alloc] init];
window_.windowLevel = UIWindowLevelAlert;
window_.frame = [UIApplication sharedApplication].statusBarFrame;
window_.x = 0;
window_.width = window_.width - window_.x;
window_.backgroundColor = [UIColor clearColor];
window_.hidden = NO;
[window_ addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(windowClick)]];
// 添加通知刷新
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(windowClick) name:FOFTopWindowNeedClickNotification object:nil];;
});
}
对外提供show和hide类方法
+ (void)show
{
window_.hidden = NO;
}
+ (void)hide
{
window_.hidden = YES;
}
2,递归寻找展示在keyWindow上的UIScrollView控件
+ (void)searchScrollViewInView:(UIView *)superview
{
for (UIScrollView *subview in superview.subviews) {
// 如果是scrollview, 滚动最顶部
if ([subview isKindOfClass:[UIScrollView class]] && subview.isShowingOnKeyWindow) {
CGPoint offset = subview.contentOffset;
offset.y = - subview.contentInset.top;
[subview setContentOffset:offset animated:YES];
}
// 继续查找子控件
[self searchScrollViewInView:subview];
}
}
3,判断该scrollView是否和keyWindow重叠(难点)
- (BOOL)isShowingOnKeyWindow
{
// 主窗口
UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
// 以主窗口左上角为坐标原点, 计算self的矩形框
CGRect newFrame = [keyWindow convertRect:self.frame fromView:self.superview];
CGRect winBounds = keyWindow.bounds;
// 主窗口的bounds 和 self的矩形框 是否有重叠
BOOL intersects = CGRectIntersectsRect(newFrame, winBounds);
return !self.isHidden && self.alpha > 0.01 && self.window == keyWindow && intersects;
}
利用UIView对象方法转换坐标系
- (CGRect)convertRect:(CGRect)rect toView:(nullable UIView *)view;
- (CGRect)convertRect:(CGRect)rect fromView:(nullable UIView *)view;
4,在AppDelegate添加show方法
- (void)applicationDidBecomeActive:(UIApplication *)application {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
// 添加一个window, 点击这个window, 可以让屏幕上的scrollView滚到最顶部
[FOFTopWindow show];
}
** Demo代码查看git **
https://github.com/tangbin583085/TBTopWindow