2011-12-17 75 views
6

我有兩個視圖控制器。一個用於頁面控制的滾動視圖,另一個用於細節(滾動垂直)。scrollToTop不適用於iPhone,但它在iPad上工作

當我點擊iPhone上的狀態欄scrollToTop不工作,並在iPad的scrollToTop工作。

我確定scrollView.scrollToTop是YES。因爲我已經打印了。

任何人都知道是什麼導致iPhone scrollToTop不工作?

謝謝

編輯: 我嘗試通過調用滾動視圖的委託。它在iPad中被稱爲 。 (BOOL)scrollViewShouldScrollToTop:(UIScrollView *)scrollView

在iPhone中它不被調用。 我已經同時使用 contentScrollView.delegate = self; 但不工作:(

編輯iphone:嘗試繼承的UIWindow(不工作)

在window.h

#import <UIKit/UIKit.h> 
#import <Foundation/Foundation.h> 
@interface Window : UIWindow 
@end 

Window.m

#import "Window.h" 

@implementation Window 

- (UIView*)hitTest:(CGPoint)point withEvent:(UIEvent *)event { 
if ([event type] == UIEventTypeTouches && point.y < 250) { 
    //Send Your Notification Here 
    NSLog(@"KAROOO"); 
} 
NSLog(@"HELLOWW"); 
return [super hitTest:point withEvent:event]; 
} 
@end 

AppDelegate.h

@property (nonatomic, strong) IBOutlet Window *window; 

AppDelegate.m

@synthesize window = _window; 

回答

3

從我瞭解你同時在屏幕上2個UIScrollViews。

從我所看到的&有經驗的, 在具有2個UIScrollView並行的iOS 4上給出了未定義的行爲。其中一個scrollview可以滾動,有時可以滾動,有時也可以不滾動。

在iOS 5,如果你有2個UIScrollViews並排然後在狀態欄攻「卷軸頂部」

「你的水龍頭下的」正確的UIScrollView我不知道這是如何工作的,但這已經我的觀察。 iOS 5很聰明!

如果你的UIScrollViews高於其他,那麼可能行爲是未定義的。我沒有檢查過。

希望這會有所幫助。

如果你想讓它工作,那麼這裏是一個可能的解決方案。

子類 或添加一個類別上 一個UIWindow並添加一個手勢識別/或實現則hitTest僅用於Y < 20檢測自來水等,然後把它在你的viewController發送通知和聽這和編程滾動UIScrollView頂部。

編輯

在一個UIWindow子類中實現這個(新iPad)

- (UIView*)hitTest:(CGPoint)point withEvent:(UIEvent *)event { 
    if ([event type] == UIEventTypeTouches && point.y < 20 && [[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationPortrait) { 
     NSLog(@"Portrait"); 
    } else if ([event type] == UIEventTypeTouches && point.y > 1004 && [[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationPortraitUpsideDown) { 
     NSLog(@"PortraitUpsideDown"); 
    } else if ([event type] == UIEventTypeTouches && point.x < 20 && [[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationLandscapeLeft) { 
     NSLog(@"LandscapeLeft"); 
    } else if ([event type] == UIEventTypeTouches && point.x > 748 && [[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationLandscapeRight) { 
     NSLog(@"LandscapeRight"); 
    } 
    return [super hitTest:point withEvent:event]; 
} 
+0

在我的iPhone ios 5不工作也。在ipad 4.3和5都在工作。我如何在UIWindow上添加一個類別並添加手勢識別器? – 2011-12-18 05:42:12

1

在同一屏幕上如果你有一個以上UIScrollView實例(包括子類如UITableView等),你需要定義一個應該對scrollToTop事件起作用的元素。這意味着,您需要通過這些實例並將它們的scrollToTop屬性設置爲NO,除非您希望這樣做。對我來說,它一直工作。如果沒有,那是因爲其他UIScrollView,我不知道它在那裏。在那種情況下,我只是通過遍歷所有子視圖遞歸地禁用了屬性,然後它開始按照預期工作。 乾杯。 :)

0

這是我的解決方案基於NSIntegerMax的答案。這樣我們就不必關心方向或硬編碼值。

聲明這個宏

#define isInStatusBar(frame, point) (point.x >= frame.origin.x && point.x <= (frame.origin.x+frame.size.width) && point.y >= frame.origin.y && point.y <= (frame.origin.y+frame.size.height)) 

且實施方法:

- (UIView*)hitTest:(CGPoint)point withEvent:(UIEvent *)event 
{ 
    CGRect frame = [[UIApplication sharedApplication] statusBarFrame]; 

    if ([event type] == UIEventTypeTouches && isInStatusBar(frame, point)) { 
     [[NSNotificationCenter defaultCenter] postNotificationName:kNotificationAppDidTouchStatusBar object:nil]; 
    } 

    return [super hitTest:point withEvent:event]; 
} 
1

不知道蘋果公司在抽菸,但在設計上,iPhone上的行爲是從iPad上明顯不同。 Apple's documentation不包括「特殊Condsideration」注意如下...

在iPhone上,滾動到頂部的姿態沒有效果,如果有更多的 比一個滾動屏幕上查看有scrollsToTop設爲是。

所以在iPhone上,你必須確保有永遠只能一個的UIScrollView(或的UITableView/UICollectionView/UIWebView的等),有它scrollsToTop = YES

由於scrollsToTop默認爲YES,因此您必須爲UIScrollView的顯式設置scrollsToTop = NO,您不想在用戶點擊狀態欄時進行滾動。

相關問題