美文网首页
点击状态栏回滚到顶部

点击状态栏回滚到顶部

作者: 怪兽密保 | 来源:发表于2016-10-19 18:48 被阅读0次

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

    .h文件声明方法
    <pre>

    • (void)show;
    • (void)hide;
      </pre>

    .m文件实现方法
    <pre>
    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];
      

      }
      }

    </pre>

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

    相关文章

      网友评论

          本文标题:点击状态栏回滚到顶部

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