0

How to make navigation bar transparent with a back button and the webView will display until the edge of the status bar.

My webview constraints are all set to the edge of Superview

I have tried this but I got the following results. Pic

I have also tried the following code at appdelegate.m

UINavigationBar *navigationBarAppearance = [UINavigationBar appearance];
navigationBarAppearance.backgroundColor = [UIColor clearColor];
[navigationBarAppearance setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
navigationBarAppearance.shadowImage = [[UIImage alloc] init];

[[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -60) forBarMetrics:UIBarMetricsDefault];

But the webview also does not load until the status bar. It will stop at the navigation bar. Pic1

I have run out of idea, please help.

Displaying the WebView Constraints, it just won't display pass the Navigation bar.

pics

Community
  • 1
  • 1
Hanz Cheah
  • 761
  • 5
  • 15
  • 44
  • have you checked your constraint of webview? – Jasmeet Kaur Mar 13 '18 at 02:17
  • Yes webview constraint are all set to the edge of Superview. Top, Bottom, Trailing and Leading – Hanz Cheah Mar 13 '18 at 02:31
  • can you check it with UI Inspector it looks like webview is not at the edge or try to give some -ve top – Jasmeet Kaur Mar 13 '18 at 02:33
  • Sorry Jasmeet, can you please elaborate how to check with UI Inspector. I have tried negative values at the top. It works but then different phones will have different top negative values and the display is not consistent over all phones. – Hanz Cheah Mar 13 '18 at 02:41
  • please check the answer below it will work on all the screen as we will set the constraint programmatically – Jasmeet Kaur Mar 13 '18 at 02:52

1 Answers1

1

You can get navbar height by, This will work for all screens

let height  =   self.navigationController.navigationBar.frame.size.height

and you need to set the top constraint of your webview to -ve to this value. first, take the reference of the top constraint say

@IBoutlet weak var topRefWebView:NSLayoutConstraint!

set its value at view will appear

let height  =   self.navigationController.navigationBar.frame.size.height
topRefWebView = -1 * height
Jasmeet Kaur
  • 1,538
  • 14
  • 16
  • i see how you are doing it, I haven't try but I think it will work. Is there a way to set it at appDelegate? – Hanz Cheah Mar 13 '18 at 03:10
  • why you want to set it at appDelegate? – Jasmeet Kaur Mar 13 '18 at 03:11
  • So it applies to all my webView? – Hanz Cheah Mar 13 '18 at 03:29
  • You can create a webview class say "MyWebViewClass" inheriting UIWebView and instead of creating UIWebView Object create MyWebViewClass object and in MyWebViewClass set the topRefWebView constraint. Adding code in AppDelegate in not a good code practice and should be avoided – Jasmeet Kaur Mar 13 '18 at 03:34
  • I just realise the Navigation bar has EQUAL height of 44 points. So I just adjust my constant of my webView to -44 and now it all works in all phones. Thank you – Hanz Cheah Mar 13 '18 at 04:15
  • I think there is a bug in xCode, while I was fiddling with in the Story Board. Some of my VC's Seague changes to Present Modally, I am sure I did not touch any of them. I thought what happened to all my back button is gone. I have to manually Change it back to Push, weird. – Hanz Cheah Mar 13 '18 at 04:33