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
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 !
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];
}
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];
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.
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;
}
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!