ios-自定义点击状态栏滚回顶部

点击状态栏滚回顶部这个功能是系统自带的,只需要设置self.scrollView.scrollsToTop = YES即可,

但是这个属性有一个前提是窗口下必须只有一个可滚动的View才有效果,这时候就需要自定义创建一个窗口

来完成这个功能。

添加窗口

  • AppDelegate创建一个新的窗口必须给这个窗口设置一个根控制器,否则会报错,这里可以通过dispatch_after来给添加窗口一个延时就可以不设置根控制器
  • 窗口是有级别的windowLevel,级别越高就越显示在顶部,如果级别一样,那么后添加的创建显示在顶部.级别分为三种,UIWindowLevelAlert > UIWindowLevelStatusBar > UIWindowLevelNormal,接下来创建一个创建并且添加一个监控
 1 /**
 2  * 显示顶部窗口
 3  */
 4 + (void)show
 5 {
 6     dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.25 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
 7         topWindow_ = [[UIWindow alloc] init];
 8         topWindow_.windowLevel = UIWindowLevelAlert;
 9         topWindow_.frame = [UIApplication sharedApplication].statusBarFrame;
10         topWindow_.backgroundColor = [UIColor clearColor];
11         topWindow_.hidden = NO;
12         [topWindow_ addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(topWindowClick)]];
13     });
14 }
15
16 /**
17  * 监听顶部窗口点击
18  */
19 + (void)topWindowClick
20 {
21     UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
22     [self searchAllScrollViewsInView:keyWindow];
23 }
24
25 /**
26  * 找到参数view中所有的UIScrollView
27  */
28 + (void)searchAllScrollViewsInView:(UIView *)view
29 {
30     // 递归遍历所有的子控件
31     for (UIView *subview in view.subviews) {
32         [self searchAllScrollViewsInView:subview];
33     }
34
35
36     // 判断子控件类型(如果不是UIScrollView,直接返回)
37     if (![view isKindOfClass:[UIScrollView class]]) return;
38
39     // 找到了UIScrollView
40     UIScrollView *scrollView = (UIScrollView *)view;
41
42     // 判断UIScrollView是否和window重叠(如果UIScrollView跟window没有重叠,直接返回)
43     if (![scrollView bs_intersectsWithAnotherView:nil]) return;
44
45     // 让UIScrollView滚动到最前面
46     // 让CGRectMake(0, 0, 1, 1)这个矩形框完全显示在scrollView的frame框中
47     [scrollView scrollRectToVisible:CGRectMake(0, 0, 1, 1) animated:YES];
48 }

注意有个uiscrollview的分类,里面加上这个方法。

- (BOOL)bs_intersectsWithAnotherView:(UIView *)anotherView
{
    if (anotherView == nil) anotherView = [UIApplication sharedApplication].keyWindow;

    // 判断self和anotherView是否重叠
    CGRect selfRect = [self convertRect:self.bounds toView:nil];
    CGRect anotherRect = [anotherView convertRect:anotherView.bounds toView:nil];
    return CGRectIntersectsRect(selfRect, anotherRect);
}

  以上方法可以判断不是同一坐标系的2个View是否重叠,并且如果anotherView为空的话,就返回窗口

在监听方法中加入以上代码即可完成功能  
时间: 2024-08-08 15:46:27

ios-自定义点击状态栏滚回顶部的相关文章

iOS UITableView中点击状态栏无法回滚到顶部

// When the user taps the status bar, the scroll view beneath the touch which is closest to the status bar will be scrolled to top, but only if its `scrollsToTop` property is YES, its delegate does not return NO from `shouldScrollViewScrollToTop`, an

iOS开发-点击状态栏scrollView回到顶部失效解决办法

若当前的ViewController中只有一个scrollView,点击状态栏,该scrollView就会滚动到顶部.但当ViewController中有多个scrollView时,就不灵了!这个时候,怎么去兼容呢? UIScrollView有这么一个属性scrollsToTop.按住command,点击scrollsToTop进去你会看到关于这个属性的注解 On iPhone, we execute this gesture only if there's one on-screen scro

ios设置点击状态栏返回到顶部

在一个scrollview中横向有多个tableview,点击状态栏,tableview要返回到顶部要将所有scrollview 以及tableview 的scrollToTop设置为NO.显示的tableview的scrollToTop设置为TRUE. @property(nonatomic,strong) NSMutableArray *tdoclist; //设置一个数组,存储tableview self.tdoclist=[NSMutableArray arrayWithCapacity

UITableView 或 UIScrollView 点击状态栏列表回到顶部

整理来自互联网- 这是tableView继承的scrollView的一个属性 scrollsToTop. 官方说明是这样的: // When the user taps the status bar, the scroll view beneath the touch which is closest to the status bar will be scrolled to top, but only if its `scrollsToTop` property is YES, its del

自定义点击状态栏返回

点击屏幕最上方状态栏(status)时,如果当前是tableview或者是可以滚动的控件,就可以滚动到最上方的位置; #import "LZPStatuesClickWindow.h" //定义一个全局变量 //整个程序的生命周期都存在; UIWindow * _statueWindow; @interface LZPStatuesClickWindow () @end @implementation LZPStatuesClickWindow +(void)show{ //需要在in

关于tableView点击状态栏列表回到顶部的说明

怎么验证是否有开发经验? 这是tableView继承的scrollView的一个属性   scrollsToTop. 官方说明是这样的: // When the user taps the status bar, the scroll view beneath the touch which is closest to the status bar will be scrolled to top, but only if its `scrollsToTop` property is YES, i

iOS点击状态栏返回顶部问题。

在适配点击状态栏返回顶部的时候,有一个viewcontroller里面有一个UITableView和一个UITextView,UITableView的cell里面没有UIScrollView和UITableView,点击状态栏没有返回顶部,后来想了很久,才想起UITextView是可以滑动的,它是继承于UIScrollView<UITextInput>的,需要将UITextView 的scrollsToTop设置为NO.

iOS 点击状态栏回到顶部

@property(nonatomic) BOOL scrollsToTop; // default is YES. UIScrollView 的scrollsToTop默认为YES,当页面里仅一个UIScrollView的时候 ,不需要做任何操作,点击状态栏就可以回到顶部 当页面上有多个UIScrollView时,此时点击状态栏 不会回到顶部 只需要把需要回到顶部的UIScrollView的scrollsToTop设置为YES,其余的设置为scrollsToTop=NO即可

点击状态栏让tableview回到顶部最简单的方法

先看官方解释,如图: 官方说一个屏幕中只能允许一个scrollsTop = YES;不然就不能滚回顶部了!! 最简单的方法: 那么就让一个屏幕中只存在一个scrollsTop = YES就可以了, 其他的scrollsTop = NO;那么就可以默认点击状态栏tableview回到顶部! 完毕!!! ************************************************************************************************* 附