点击状态栏回滚到顶部

一.新建一个类(继承NSObject)

.h文件声明方法

  • (void)show;
  • (void)hide;

.m文件实现方法


static UIWindow *window_;

  • (void)initialize
    {
    window_ = [[UIWindow alloc] init];
    window_.frame = CGRectMake(0, 0, UIWidth, 20);
    window_.windowLevel = UIWindowLevelAlert;
    [window_ addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(windowClick)]];
    }

  • (void)show
    {
    window_.hidden = NO;
    }

  • (void)hide
    {
    window_.hidden = YES;

}

/**

  • 监听窗口点击
    */
  • (void)windowClick
    {
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    [self searchScrollViewInView:window];

}

  • (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];
    

    }
    }

注意:isShowOnKeyWindow方法请参参我的另一篇文章(判断View是否显示在屏幕上)http://www.jianshu.com/p/a9ab004e0619

你可能感兴趣的:(点击状态栏回滚到顶部)