Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

UIWebView appears behind previous view

I'm working the extra credit for the Photo Bombers app of the new iOS tutorial. I decided that instead of opening the links in Safari that I wanted to open my links in a UIWebView so I didn't have to switch back and forth between apps. I got the UIWebView added to the app, and it appears as expected (with the exception of some sizing issues, I think); however, I've noticed that the web view covers all of the previous view with the exception of the photo. The photo seems to supersede the web view that was just loaded.

/* Code to load the WebView in THMetadataView.m */
@property (nonatomic) UIWebView *webView;

- (void) setPhoto:(NSDictionary *)photo {
        ...
        ...

        [self.usernameButton addTarget:self
                              action:@selector(openUser:)
                 forControlEvents:UIControlEventTouchUpInside];

       ...
       ...
}

- (void)openUser:(id)sender {
    self.webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height)];

    NSLog(@"username: %@", self.photo[@"user"][@"username"]);

    NSString *urlString = [NSString stringWithFormat:@"http://www.instagram.com/%@", self.photo[@"user"][@"username"]];

    NSURL *url = [NSURL URLWithString:urlString];

    [self.webView loadRequest:[NSURLRequest requestWithURL:url]];

    [self addSubview:self.webView];
}

Here's what it looks like: http://imgur.com/TbWziey

If anyone can help it would be greatly appreciated.

5 Answers

try adding your webview to the main window like so

 [self.webView loadRequest:[NSURLRequest requestWithURL:url]];

UIWindow* mainWindow = [[UIApplication sharedApplication] keyWindow];
[mainWindow addSubview:self.webView ];

Im guessing (could be wrong who knows) thats most likely because the photo view is a modal view. you may have to dismiss that modal view first, then present the web view

Then I'll have to rebuild that view when the web view get's dismissed? Is that common practice or is that just something that I need to do because of the way the app is constructed?

thats just the nature of modal views in anything really, not just iOS. they usually require the user to interact with it in some way before they go away. I havent really worked with them that much here with iOS, so like I said I could be wrong and maybe there is some way to get your web view to the top of the stack.

Also, crazy fast reply! I appreciate it!

YES! That worked. Just need to add that to a navigation controller or something with a close button to show the previous view. Any adverse effects you can think of with this approach? I'm a web dev by trade so iOS is a bit foreign to me but I'm starting to pick it up bit by bit.

yeah I would probably just add a UIButton with an X or something to the view. As far as adverse effects, I dont really know haha. To me it seems like a bit of a hack but im not really sure how else to get around that modal view without dismissing it first, showing the webview, then bring that modal back up when your done with the webview.