简体   繁体   中英

UIWebView not won't “scrollsToTop” on iOS 7?

I have two UIWebView in a single ViewController in order to preload one in the background for the "next page".

Since opening the project in Xcode 5, these no longer scroll to the top when the status bar is tapped.

The App is otherwise working fine with iOS 7 so I don't know what's wrong. When one of the UIWebView is hidden, I also setScrollsToTop:NO ... so there's clearly something I'm missing.

Any ideas?

I've written a simple class for this specific issue. In my app we're having multiple webviews and scrollviews. This makes it all much easier.

https://gist.github.com/hfossli/6776203

Use this category:

UIView+LogViewHierarchy.h

#import <UIKit/UIKit.h>

@interface UIView (LogViewHierarchy)

-(void) logViewHierarchy;

@end

UIView+LogViewHierarchy.m

#import "UIView+LogViewHierarchy.h"

@implementation UIView (LogViewHierarchy)


-(void) logViewHierarchy
{

    NSLog(@"%@%@", NSStringFromClass(self.class), NSStringFromCGRect(self.frame));
    [self logSubviews:self depth:1];
}

-(void) logSubviews:(UIView*)view depth:(NSInteger)depth
{
    for (UIView *subview in view.subviews) {
        if ([subview isKindOfClass:[UIScrollView class]]) {
            NSLog(@"%@%@%@ - scrollsToTop: %@", [self paddingString:depth], NSStringFromClass(subview.class), NSStringFromCGRect(subview.frame), ((UIScrollView*)subview).scrollsToTop ? @"YES" : @"NO");
        } else {
            NSLog(@"%@%@%@", [self paddingString:depth], NSStringFromClass(subview.class), NSStringFromCGRect(subview.frame));
        }
        [self logSubviews:subview depth:depth+1];
    }
}

-(NSString*) paddingString:(NSInteger)depth
{
    return [@"" stringByPaddingToLength:depth*2 withString:@" " startingAtIndex:0];
}


@end

Then find your app's root view controller and add:

-(void) viewDidAppear:(BOOL)animated
{
    [self.view logViewHierarchy];
}

You will be able to see all UIScrollViews and their scrollsToTop properties. When you manage to make your output look like there is only 1 UIScrollView that has scrollsToTop == YES , then your scrollsToTop gesture should work fine.

In my case, logViewHierarchy reported that all your scroll views have scrollsToTop = NO , and you didn't know why, because I explicitly set them to YES . It turned out that I left this UIScrollView category inside my project, and it overloaded some methods on all my UIScrollViews. When I removed that category from my project, I was able to get correct output from logViewHierarchy . Then I played around with code until I managed to get only one scrollsToTop = YES .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM