iOS7 Webview initial scroll position under Navigation bar

后端 未结 8 2606
[愿得一人]
[愿得一人] 2021-02-18 22:50

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

相关标签:
8条回答
  • 2021-02-18 23:19

    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 !

    0 讨论(0)
  • 2021-02-18 23:21

    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];
    }
    
    0 讨论(0)
  • 2021-02-18 23:22

    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];
    
    0 讨论(0)
  • 2021-02-18 23:26

    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.

    0 讨论(0)
  • 2021-02-18 23:26

    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;
    }
    
    0 讨论(0)
  • 2021-02-18 23:30

    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!

    0 讨论(0)
提交回复
热议问题