7

So I have been trying to get this simple behavior working on my iPhone app now for a while. I have a nav bar at the top and a tab bar at the bottom. I am loading all of my content in a webview, which I want to fit between the two. I have posted about this issue twice already,

both here: IOS View still does not load in the correct position

and here: Programmatically setting content to fill screen between tab bar and nav controller

Currently, my webview looks and performs fine on iPhone 4 and 4s, the issue arises on the 5s. When the screen finishes loading, it looks like the image here: http://grosh.co/screen.jpg . This occurs on both the simulator as well as devices.

Upon further inspection, I discovered that the UIWebView is in fact spanning correctly (the grey areas you see are the webview). The smaller content is actually a UIWebBrowserView, which I am having a hard time finding any information on.

I have the code I am using below, as well as a log output to insure all the numbers are correct. My questions are,

1) Is there a better way to set the webview to span the screen area between the top and bottom bars for all devices?

2) If not, is there a way to set the UIWebBrowser to span the entire webview?

I tried iterating over the subviews of the webview as described here: http://normansoven.com/ios-webview/#.VHyOUr6Jnww but the webBrowserView was never seen.

Teh Codez:

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.    


CGFloat viewheight = self.view.bounds.size.height;
NSLog(@"Height1:%f",viewheight);

CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat viewheight2 = screenRect.size.height;
NSLog(@"Height2:%f",viewheight2);

CGFloat height = self.view.frame.size.height;
NSLog(@"Height3:%f",height);


CGFloat navBarHeight = self.navigationController.navigationBar.frame.size.height;
NSLog(@"NAVHeight:%f",navBarHeight);

CGFloat tabBarHeight = self.tabBarController.tabBar.frame.size.height;
NSLog(@"TABHeight:%f",tabBarHeight);

CGFloat statusbarheight = [UIApplication sharedApplication].statusBarFrame.size.height;
NSLog(@"StatbarHeight:%f",statusbarheight);

CGFloat spaceToRemove = navBarHeight + tabBarHeight + statusbarheight;
    NSLog(@"NAVHeight+TABHeight:%f",spaceToRemove);
CGFloat newHeight = viewheight - spaceToRemove;
        NSLog(@"NewHeight:%f",newHeight);

CGRect frame =  CGRectMake(0 , navBarHeight + statusbarheight, self.view.frame.size.width, newHeight);

self.webView = [[UIWebView alloc]initWithFrame:frame];


//self.webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];


[self.view addSubview:self.webView];

self.webView.scalesPageToFit = true;
NSURL *url = [NSURL URLWithString:@"http://example.com/finnaRoot/index.php"];
NSURLRequest *requestURL = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:requestURL];
self.webView.scrollView.showsVerticalScrollIndicator = false;


self.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"logo.png"]];

self.webView.delegate = self;
self.tabBarController.delegate = self;
self.webView.scrollView.delegate = self;

}

Log output, from an iPhone 5s

2014-11-26 17:19:21.184 Finna[14617:1765689] Height1:568.000000
2014-11-26 17:19:21.185 Finna[14617:1765689] Height2:568.000000
2014-11-26 17:19:21.185 Finna[14617:1765689] Height3:568.000000
2014-11-26 17:19:21.185 Finna[14617:1765689] NAVHeight:44.000000
2014-11-26 17:19:21.185 Finna[14617:1765689] TABHeight:49.000000
2014-11-26 17:19:21.185 Finna[14617:1765689] StatbarHeight:20.000000
2014-11-26 17:19:21.185 Finna[14617:1765689] NAVHeight+TABHeight:113.000000
2014-11-26 17:19:21.186 Finna[14617:1765689] NewHeight:455.000000
Community
  • 1
  • 1
Voltron
  • 431
  • 7
  • 19
  • So the latest iOS update fixed this issue, which leads me to believe it was a bug. I will add the workaround code I used below when I get a chance. It worked and the app was accepted to the store. – Voltron Feb 16 '15 at 08:35

2 Answers2

8

I've encountered with the same problem. And found simple fix. The culprit is UIViewController's property automaticallyAdjustsScrollViewInsets. UIViewController reference says:

Default value is YES, which allows the view controller to adjust its scroll view insets in response to the screen areas consumed by the status bar, navigation bar, and toolbar or tab bar.

It means that UIViewController adds that top gap to UIWebView's scroll view even if you properly set up webview frame or add autolayout constraints. To overcome this behaviour just

Set to NO if you want to manage scroll view inset adjustments yourself, such as when there is more than one scroll view in the view hierarchy.

"manage scroll view inset adjustments your self" in our context means that we won't add any insets ;)

same issue with any scroll view, i.e. Blank space at top of UITextView in iOS 10 https://stackoverflow.com/search?q=automaticallyAdjustsScrollViewInsets

Community
  • 1
  • 1
Ilia
  • 1,434
  • 15
  • 22
4

This issue has also resurfaced with iPhone X.

With iOS 11, UIScrollView now has the property contentInsetAdjustmentBehavior.

webview.scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;

can correct this issue.

David Skelly
  • 617
  • 6
  • 5
  • 1
    Worked out great for me, but needed an additional wrapping with @available: if (@available(iOS 11.0, *)) { self.webView.scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever; } – BPH Dec 16 '17 at 21:42