ScrollToTop does not work on iPhone, but works on iPad

I have two view controllers. One scrollview for page management and one for details (vertical scroll).

When I click the status bar on the iPhone, scrollToTop does not work, and scrollToTop works on the iPad.

I am sure scrollView.scrollToTop is YES. because I already printed it.

Does anyone know why iPhone scrollToTop is not working?

thanks

Edit: I am trying to invoke the scrollView delegate. on the iPad, this delegate called him. - (BOOL) scrollViewShouldScrollToTop: (UIScrollView *) scrollView

it’s not called on the iPhone. I already use both contentScrollView.delegate = self; But iphone does not work :(

Edit: Try to subclass UIWindow (not working)

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; 
+6
source share
4 answers

From what I understand, you have 2 UIScrollViews simultaneously on the screen.

From what I saw and experienced, On iOS 4, which has 2 UIScrollViews, side by side gives undefined behavior. One of scrollview can scroll, sometimes others, sometimes too.

In iOS 5, if you have 2 UIScrollViews nearby, then clicking on the status bar “scrolls up” to the right of the UIScrollView “under your faucet”

I do not know how this works, but that was my observation. iOS 5 smart!

If your UIScrollViews are one above the other, then the behavior is probably undefined. I did not check.

Hope this helps.

If you want to make it work anyway, then this is a possible solution.

Subclass or Add a category to UIWindow and add a gesture recognizer / or implement a hitTest that detects touch only for Y <20, etc., Then send a notification and listen to it in your viewController and programmatically scroll through the UIScrollView at the top.

EDIT

Embed this in a subclass of 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]; } 
+3
source

If you have more than one instance of UIScrollView (including subclasses, such as UITableView etc) on the same screen, you need to determine which one should act in the scrollToTop event. This means that you need to go through these instances and set their scrollToTop property to NO, except the one you want to do. It always worked for me. If this is not the case, then due to some other UIScrollView I did not know this. In this case, I just went through all the sub-items, turning off thet property recursively, and then started working as expected. Greetings. :)

+1
source

I don’t know what Apple smoked, but the design behavior on the iPhone is noticeably different from the iPad. Apple's documentation contains a “Special Summary” note as follows ...

On iPhone, the gesture of scroll gestures is not affected if there is more than one scroll screen on the screen with the ToTop scroll options on YES.

So, on iPhone, you have to make sure that there is only one UIScrollView (or UITableView / UICollectionView / UIWebView, etc.) that has scrollsToTop = YES .

Since scrollsToTop defaults to YES, you must explicitly set scrollsToTop = NO for the UIScrollView, which you don't want to scroll when the user deletes the status bar.

+1
source

Here is my solution based on NSIntegerMax answer. Thus, we do not need to care about orientation or have hardcoded values.

declare this macro

 #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)) 

and implement the method:

 - (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]; } 
0
source

Source: https://habr.com/ru/post/903965/


All Articles