0

I thought I had this cleared up but apparently not. I am using the code below to set a webview to appear between the top nag bar and tab bar at the bottom. This happens in my viewDidLoad() method. It was working fine on all simulators I had tested it on, however when I tested a friend's iPhone 4s running 7.1.1 the webview rendered spanning the entire height of the screen, covered by both the top nag bar and bottom tab bar.

How to I get the desired behavior across all devices and OS above 7?

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/notifications.php"];
NSURLRequest *requestURL = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:requestURL];
self.webView.scrollView.showsVerticalScrollIndicator = false;

self.navigationController.navigationBar.titleTextAttributes = @{UITextAttributeTextColor : [UIColor whiteColor]};

[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.webView.delegate = self;
self.webView.scrollView.delegate = self;
Voltron
  • 431
  • 7
  • 19

1 Answers1

1

It's because you are spanning the entire view in your rect make you need to subtract the height and width of the tab and nav bar

take a look at this example

- (void)viewDidLoad {
    [super viewDidLoad];

    CGFloat viewheight = self.view.frame.size.height;
    CGFloat navBarHeight = self.navigationController.navigationBar.frame.size.height;
    CGFloat tabBarHeight = self.tabBarController.tabBar.frame.size.height;
    CGFloat spaceToRemove = navBarHeight + tabBarHeight;
    CGFloat newHeight = viewheight - spaceToRemove;

    NSLog(@"%f",newHeight);
    NSLog(@"%f",self.view.frame.size.height);


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

    UIView *newView = [[UIView alloc]initWithFrame:frame];
    newView.backgroundColor = [UIColor redColor];
    [self.view addSubview:newView];


}
  • it's unfortunate Apple hasn't fixed this yet. I mean I can't think of a scenario where I would want my content covered by a bar or keyboard. –  Nov 22 '14 at 23:33
  • It is a shame this isn't simpler. Why does the above code work for other devices? Ill test this code before the night ends and see what happens. Thanks – Voltron Nov 22 '14 at 23:39
  • I'm not sure why exactly for some reason the new OS tries to cover everything it can be pretty frustrating to say the least. I wish I new of an easier way, but I sure haven't thought of one yet. –  Nov 22 '14 at 23:41
  • So I tested this and it does not seem to work on the iPhone simulator. Ill run a device test in the morning but it appears to as seen in the image here http://grosh.co/screen.jpg ... – Voltron Nov 23 '14 at 06:50
  • Yeah same behavior on the device @CSpell ... I would imagine this would work for the device that spurred the issue originally though. Think there is a way to check for that? – Voltron Nov 23 '14 at 16:23
  • that's wild... i'm not sure.. If I build a new project select the view controller and select editor -> embed in tab bar -> editor embed in nav controller and then copy and paste the above code in controller it works. –  Nov 23 '14 at 16:32
  • It could be that you are aligning to the IB grid lines on IOS 8 in IB and the lines are not recognized in IOS 7 maybe?.? I'm not real sure what's going on there. Are you getting any constraint errors on console? –  Nov 23 '14 at 16:35
  • No constraint errors on the console. My web views do not actually exist on the IB, I set them up and initialize them in my viewDidLoad method – Voltron Nov 23 '14 at 16:40
  • I'm not having any luck recreating that at all. Would you mind posting your constraints. –  Nov 23 '14 at 16:56
  • what happens if you copy and paste on a brand new project? same results? –  Nov 23 '14 at 17:00
  • I added my original code setting up the webview above. It appears in the viewDidLoad method. I will try a new project today and see if I can't get your example code to work. – Voltron Nov 23 '14 at 17:28
  • I can't seem to replicate this. Even when I copy and paste your code and just use the frame from my code (actually I have to add margins to make it even smaller.) I hate to say it ,but being as you're already using a property maybe you should connect the webView in IB and just pin it to the layout guides. –  Nov 23 '14 at 18:24
  • also i don't think it makes a difference in this case, but shouldn't super be the first call? I was under that impression. Do you know if it's mentioned in the docset? –  Nov 23 '14 at 18:31
  • Hmm well I just tried removing all size declarations and defining the webview through interface builder. Same behavior. Appears fine on my 5s, but when tested on a 4s, the webview is covered by the tab bar and nav bar. I adjusted the super as you said. – Voltron Nov 23 '14 at 21:53
  • Well if you added it on IB you shouldn't have to change the frame the constraints should be at the guide line I would think even on older devices. It's strange that manually forcing the frame size doesn't fix the issue though. I've been thinking about this a lot today and I just can't seem to come up with an answer. Are you setting up the frame and the tab bar measurements after the tab/nav bar are created? I know it's a stupid question, but I have to ask as an extra set of eyes just for the sake of thoroughness. –  Nov 24 '14 at 04:03
  • I tried both with constraints and without when using the IB... still no consistency in my results. The tab/nav bar I set up in the IB, I did not do them programmatically. The sizes they return appear to be correct, so idk if thats the issue. I appreciate you giving this so much thought! Ill be asking a few professors tomorrow so hopefully they might be able to shed some light. Its probably easier to solve with Xcode right in front of you. – Voltron Nov 24 '14 at 05:06
  • Ok well I do hope you will post the answer when you find it. If I can think of a reason this is happening or find something to fix it I'll post it. –  Nov 24 '14 at 06:32
  • Okay so update! I ran more tests today, logging the results of the height, and adjusting the position on the screen to see different behaviors. I believe the code you gave me was indeed working... all the screen heights returned are correct in every test, and the sizes are correct as well. When I adjusted the position of the view, I realized what was wrong. In the image I posted above, the grey areas above and below ARE actually the webview! So the question becomes, why is the actual webpage loading smaller than the webview itself?? – Voltron Nov 25 '14 at 04:59