iOS開發-點擊狀態列scrollView回到頂部失效解決辦法

來源:互聯網
上載者:User

標籤:

若當前的ViewController中只有一個scrollView,點擊狀態列,該scrollView就會滾動到頂部。但當ViewController中有多個scrollView時,就不靈了!這個時候,怎麼去相容呢?

UIScrollView有這麼一個屬性scrollsToTop。按住command,點擊scrollsToTop進去你會看到關於這個屬性的註解

On iPhone, we execute this gesture only if there‘s one on-screen scroll view with `scrollsToTop` == YES. If more than one is found, none will be scrolled.

現在知道為什麼不靈了吧!!!

最近做項目,由於一個橫屏的scrollView裡面放置了多個tableView,但點擊狀態列仍要求tableView滾動到頂部,所以要克服這個問題!

雖然"魏則西事件"讓我們大家很鄙視百度,但在Google還進不來的情況下,我默默的百度了一下!

。。。。。。。。。。。。。。。。。。。。。。。。

問題解決後,我總結了一下。

思路是這樣的:

1 追蹤UIStatusBar的touch事件,然後響應該事件。不要直接向statusBar添加事件,因為並沒有什麼卵用!我也不知道為什麼沒有什麼卵有!

2 找到UIApplication的keyWindow,遍曆keyWindow的所有子控制項。如果是scrollView,而且顯示在當前keyWindow,那麼將其contentOffset的y值設定為原始值(0)。這裡會用到遞迴去進行遍曆。

首先我們追蹤UIStatusBar的觸摸事件,需要在AppDelegate裡面加入以下代碼

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {    [super touchesBegan:touches withEvent:event];    CGPoint location = [[[event allTouches] anyObject] locationInView:self.window];    CGRect statusBarFrame = [UIApplication sharedApplication].statusBarFrame;    if (CGRectContainsPoint(statusBarFrame, location)) {        [self statusBarTouchedAction];    }}

然後在statusBarTouchedAction方法中將顯示在當前keyWindow裡面的scrollView滾動到頂部

- (void)statusBarTouchedAction {    [JMSUIScrollViewTool scrollViewScrollToTop];}

下面來看JMSUIScrollViewTool

#import <Foundation/Foundation.h>@interface JMSUIScrollViewTool : NSObject+ (void)scrollViewScrollToTop;@end
#import "JMSUIScrollViewTool.h"@implementation JMSUIScrollViewTool+ (void)scrollViewScrollToTop {    UIWindow *window = [UIApplication sharedApplication].keyWindow;    [self searchScrollViewInView:window];}+ (void)statusBarWindowClick {    UIWindow *window = [UIApplication sharedApplication].keyWindow;    [self searchScrollViewInView:window];}+ (BOOL)isShowingOnKeyWindow:(UIView *)view {    UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;    CGRect newFrame = [keyWindow convertRect:view.frame fromView:view.superview];    CGRect winBounds = keyWindow.bounds;    BOOL intersects = CGRectIntersectsRect(newFrame, winBounds);    return !view.isHidden && view.alpha > 0.01 && view.window == keyWindow && intersects;}+ (void)searchScrollViewInView:(UIView *)supView {    for (UIScrollView *subView in supView.subviews) {        if ([subView isKindOfClass:[UIScrollView class]] && [self isShowingOnKeyWindow:supView]) {            CGPoint offset = subView.contentOffset;            offset.y = -subView.contentInset.top;            [subView setContentOffset:offset animated:YES];        }                [self searchScrollViewInView:subView];    }}@end

接下來,就讓你的項目跑起來吧!

此處應有掌聲。。。

參考:http://stackoverflow.com/questions/3753097/how-to-detect-touches-in-status-bar

        http://www.cocoachina.com/ios/20150807/12949.html

iOS開發-點擊狀態列scrollView回到頂部失效解決辦法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.