Explanation: I have a viewcontroller that contains a NavigationBar and a UIWebView. The webview is set to full screen in the .m file. On the viewcontroller, the navigationbar is arranged to the front.
What happens: When I run the app, the only thing that is visible is the webview (full screen) but the navigation bar is not there.
What's suppose to happen: This is self-explanatory but the navigationbar is suppose to be at the top with webview under it.
What it looks like
.m file
#import "SecondViewController.h"
@interface ViewController2 ()
@end
@implementation ViewController2
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIWebView *webview2=[[UIWebView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width,self.view.frame.size.height)];
NSString *url2=@"http://bithumor.co/server/trending/index1.php";
NSURL *nsurl2=[NSURL URLWithString:url2];
NSURLRequest *nsrequest2=[NSURLRequest requestWithURL:nsurl2];
[webview2 loadRequest:nsrequest2];
webview2.scrollView.bounces = NO;
[self.view addSubview:webview2];
@end
.h file
#import <UIKit/UIKit.h>
@interface ViewController2 : UIViewController
@end
Issue/Question: The navigationbar isn't visible, how can this be fixed so both the webview and navigationbar are visible?
So, I can replicate what you may be experiencing, and I've wrapped up two projects so that you can see them. Here's some screen shots, but first the code I'm using is your code in this fashion:
Create single page application
Replace the view did load with your view did load
Here's the results of using your code with a navigation bar with your code and with a navigation bar like you said:
You can download this project here: http://www18.zippyshare.com/v/DXi9wySi/file.html
Expanded Layout (same result you have):
Storyboard Layout:
So, given the above screenshots and the navigation bar being placed behind the we web view is due, in part to the fact that I don't have a navigation controller that is holding my view controller so the placement of a navigation bar is entirely useless and doesn't show up on the screen. In addition, the navigation bar wouldn't even do anything if it did show up since there's no navigation controller in the hierarchy.
Now, given this first example, here's a correction.
This is the same as the layout as above but the difference is that I've rooted the view controller in a navigation controller .. everything remains the same and in fact, I didn't even add a navigation bar to the storyboard since this is auto generated by the rooting of a view controller of a navigation controller.
You can download this project: http://www41.zippyshare.com/v/aXRlAT3a/file.html
Expanded Layout:
Storyboard Layout:
So, the difference here, again is that I'm rooting the view controller in a navigation controller.
Conclusion:
It could very well be the case that the problem you are experiencing is totally different from the problem I've explained above, but using your code, I was able to achieve the same result that you have in my first example. In my second example, I was able to use a web view, exactly as you show with your code, and I in fact DO have a navigation bar on this second example. The only thing I changed in the second example was rooting the view controller in a navigation controller.
debug your views and check whether u r root view controller is navigation controller or not. also check self.navigationController == nil
Do the UIWebview
creation in the Storyboard
: layout, constraints, navigation bar from Navigation Controller, title, referencing outlets, delegates, all of it. Every bit of setup implemented at the resource level will remove lines of code, potential bugs, testing, headaches and frustration.
Entire Class
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIWebView *webView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSString *url2=@"https://google.com";
NSURL *nsurl2=[NSURL URLWithString:url2];
NSURLRequest *nsrequest2=[NSURLRequest requestWithURL:nsurl2];
self.webView.scrollView.bounces = NO;
[self.webView loadRequest:nsrequest2];
}
@end
The solution becomes language agnostic, and works equally well in Swift & Obj-C.
► Find this solution on GitHub and additional details on Swift Recipes.
Is the ViewController2
actually in a UINavigationController
or had the UINavigationBar
been placed into the view in the Storyboard
?
If you have just dragged the UINavigationBar
into the view in the Storyboard
, and then in the viewDidLoad
method you are adding the UIWebView
(with a frame of the full UIView
of the ViewController2
), then of course you will not see the UINavigationBar
because it is being covered by the UIWebView
.
If the ViewController2
is actually in a UINavigationController
, then I will need more details to find the problem.
EDIT: A (possibly) unrelated problem:
In your Storyboard
, you already have placed a UIWebView
, which I am guessing you are connecting to the IBOutlet
of webView2
.
Why are you instantiating -another- UIWebView
in your viewDidLoad
code? You already have one and can just reference it instead of creating a new one for seemingly no reason.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With