iOS开发

ios控制器内多个scrollView实现点击status ba

2016-03-18  本文已影响153人  6d325f5426f7

当控制器内部只有一个scrollView时,系统会默认处理实现点击顶部status bar,使scrollView回滚到顶部的功能,但是当内部有多个scrollView,比如一个scrollView中嵌入了多个tableview时,功能便失效了,需要自己实现这一功能。

  #import "BSTopWindow.h"


  @implementation BSTopWindow
  static UIWindow  *window_;
  + (void)initialize
  {
  // window必须学要指定rootViewController
      UIViewController *vc = [[UIViewController  alloc] init];
      window_ = [[UIWindow alloc]         initWithFrame:CGRectMake(0, 0, BSScreenWidth, 20)];
      window_.windowLevel = UIWindowLevelAlert;
      window_.rootViewController = vc;

      [window_ addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(windowClicked)]];

  }
  + (void)show
  {
      window_.hidden = NO;
  }
   + (void)windowClicked
  {
      UIWindow *keyWindow = [UIApplication   sharedApplication].keyWindow;
      [self searchScrollViewInView:keyWindow];
  }

  + (void)searchScrollViewInView:(UIView *)view
  {
      for (UIScrollView *subView in view.subviews) {
          if ([subView isKindOfClass:[UIScrollView class]] && [self isViewInScreen:subView]) {
          CGPoint offset = subView.contentOffset;
          offset.y = - subView.contentInset.top;
        
          [subView setContentOffset:offset animated:YES];
        
           }
      [self searchScrollViewInView:subView];
      }
  }
  //可以抽成UIView的分类方法,用于判断某个控件是否正显示在屏幕上
  + (BOOL)isViewInScreen:(UIView *)view
  {
      UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;

      CGRect newFrame = [view.superview convertRect:view.frame toView:nil];
      BOOL isIntersect = CGRectIntersectsRect(keyWindow.bounds, newFrame);

      return !view.isHidden && view.alpha > 0.01 && isIntersect  && view.window == keyWindow;
  }

上面代码完成后进入程序会发现status bar不见了,是因为默认是有window的rootViewController控制status bar的样式,在info.plist文件中可以修改,

上一篇下一篇

猜你喜欢

热点阅读