iOS7 Webview initial scroll position under Navigat

2020-07-02 11:09发布

问题:

I have a webview which is scrolling as desired underneath a navigation bar.

However, when I first load the controller, the page loaded in the webview is scrolled so that it aligns with the top of the navigation bar. When I scroll the web view, the correct inset is present at the top to sit correctly, it's just the initial position that is incorrect.

How can I get the initial position to be fully scrolled to the top, including the inset?

回答1:

If you don't mind having an opaque navigation bar, then the simplest solution could be to do this in the view controller that contains your web view:

self.navigationController.navigationBar.translucent = NO;

The positioning of the frame will then adopt the same behavior as iOS6, magically!



回答2:

Rick's answer is correct except the top value should be negative, this is what worked for me!

[self.webView.scrollView setContentInset:UIEdgeInsetsMake(-44, 0, 0, 0)];
[self.webView.scrollView setScrollIndicatorInsets:UIEdgeInsetsMake(-44, 0, 0, 0)];
[self.webView.scrollView scrollRectToVisible:CGRectMake(0, 0, 1, 1) animated:NO];

I used -44, if you don't have status bar on the top you should use -64.



回答3:

The way I solved this issue is with the webViewDidFinishLoad delegate callback. In the callback method for the web view I set do all the work to make the web view's scroll view look correct.

[webView.scrollView setContentInset:UIEdgeInsetsMake(64, 0, 0, 0)];
[webView.scrollView setScrollIndicatorInsets:UIEdgeInsetsMake(64, 0, 0, 0)];
[webView.scrollView scrollRectToVisible:CGRectMake(0, 0, 1, 1) animated:NO];


回答4:

I had the same issue and fixed it. The solution is pretty simple:

Go to the storyboard, select the view controller which contains your UIWebView and open the Attributes Inspector. Here, you'll see the title "Extend Edges", just uncheck the box "Under Top Bars" and I will work !

Hope this helps !



回答5:

You can also set the content offset of the webview's scrollview in viewDidAppear:, for example:

[self.webView.scrollView setContentOffset:CGPointMake(0, -64) animated:NO];

Unfortunately, it has no effect if placed in viewWillAppear:, so when the view appears you will see a visible jump in the content as it shifts from underneath the navigation bar to its new location.



回答6:

use the below

CGRect applicationFrame = [[UIScreen mainScreen] applicationFrame];
CGFloat navHeight = self.navigationController.navigationBar.frame.size.height;
applicationFrame.origin.y = navHeight + 4;
webView = [[UIWebView alloc] initWithFrame:applicationFrame];


回答7:

My solution:

- (void)loadView {
    ...

    _offsetSet = NO;
}

- (void) viewDidLayoutSubviews {

    if (!_offsetSet) {
        [_webView.scrollView setContentOffset:CGPointMake(0, -self.topLayoutGuide.length) animated:NO];
    }
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];

    _offsetSet = YES;
}


回答8:

For a more general version of @Puran's answer, rather than hardwiring 44 or 64, get it from the topLayoutGuide. Also, if you load multiple times, you only need to change these values once:

UIEdgeInsets insets = self.myWebView.scrollView.contentInset;
if ( UIEdgeInsetsEqualToEdgeInsets(insets, UIEdgeInsetsZero)) {
    insets.top = -self.topLayoutGuide.length;
    [self.myWebView.scrollView setContentInset:insets];
    [self.myWebView.scrollView setScrollIndicatorInsets:insets];
    [self.myWebView.scrollView scrollRectToVisible:CGRectMake(0, 0, 1, 1) animated:NO];
}


标签: ios7