2

EDIT:

The UIWebView is first initialized in FirstViewController like this:

- (void)viewDidLoad {
    [super viewDidLoad];
    self.webView = [[UIWebView alloc] initWithFrame:self.view.frame];
    self.webView.delegate = self;
    [self.view addSubview: self.webView];
    NSURL *url = [NSURL URLWithString:BASE_URL];
    NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
    [self.webView loadRequest:urlRequest];
}

The reference to this UIWebView is passed on to the SecondViewController, and I add it as a subview like this:

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.webView.delegate = self;
    [self.view addSubview:self.webView];
}

This attaches a UIWebView into a View Controller's view. If I call this function inside viewDidAppear, I get a UIWebView that fits the whole screen. However, if I call it inside viewWillAppear, UIWebView only takes half the whole screen's size. I tried comparing viewController.view's sizes for viewWillAppear and viewDidAppear but they seemed to be the same.

According to this very similar post it's appropriate to resize subviews in viewDidLayoutSubviews. So I tried the following:

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    self.webView.frame = self.view.bounds;
}

but it still looks like this:

enter image description here

Community
  • 1
  • 1
Maximus S
  • 10,759
  • 19
  • 75
  • 154

3 Answers3

1

You shouldn't resize UIView in viewWillAppear. It will be resize again same as in Interface Builder. If you don't want to put in viewDidAppear, you can try put in viewDidLayoutSubviews

tuledev
  • 10,177
  • 4
  • 29
  • 49
  • Seems to be the same. viewWillAppear: 375.000000, 667.000000 viewDidAppear: 375.000000, 667.000000 NSLog(@"viewDidAppear: %f, %f", self.webView.bounds.size.width, self.webView.bounds.size.height); – Maximus S Sep 04 '15 at 04:42
  • How about in `viewDidLayoutSubviews ` ? – tuledev Sep 04 '15 at 04:43
  • `viewDidLayoutSubviews` is called three times for some reason, but all the times it's the same size as well. `375.000000, 667.000000` – Maximus S Sep 04 '15 at 04:44
  • Does it work in `viewDidAppear ` ? Or not in `viewDidAppear ` also. – tuledev Sep 04 '15 at 04:46
  • `self.automaticallyAdjustsScrollViewInsets = NO;` did the trick. Thank you so much for helping me out too! – Maximus S Sep 04 '15 at 04:47
1

You can setup the UIWebView inside viewDidLoad like so:

self.webView = [[UIWebView alloc] initWithFrame:self.view.frame];
self.webView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
self.webView.delegate = self;
[self.view addSubview:self.webView];

or adjusting its size inside didLayoutSubviews as @anhtu said:

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];

    self.webView.frame = self.view.bounds;
}
Bannings
  • 10,376
  • 7
  • 44
  • 54
  • Unfortunately, `self.webView.frame = self.view.bounds;` doesn't work. Could you look at the screenshot? – Maximus S Sep 04 '15 at 04:33
  • What's type of the `self.view`? – Bannings Sep 04 '15 at 04:37
  • `UIView*`. `self` refers to the ViewController that contains everything. – Maximus S Sep 04 '15 at 04:38
  • 1
    Try this `self.automaticallyAdjustsScrollViewInsets = NO;` inside `viewDidLoad` – Bannings Sep 04 '15 at 04:44
  • It's just a guess that you wrapping the `viewController` in `UINavigationController` and `UITabBarController`, so the `webView`'s contentInsets will to be adjusted. – Bannings Sep 04 '15 at 04:50
  • `automaticallyAdjustsScrollViewInsets` works a bit differently in iOS 8. In 7, it only applied if the controller's view property was a scrollView. In 8, it seems to apply to scroll views that are subviews of the controllers' view. – Bannings Sep 04 '15 at 04:53
0

try these..

UIView *viewAll=[[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
[self.view addSubview:viewAll];

i hope it helps..

krushnsinh
  • 874
  • 1
  • 10
  • 11