1

Ok....just doing some self education. Thought I would try and create what I thought was a simple webview of youtube. So the webview opens Youtube but the content is way off the screen. It's like the page is being delivered to a larger device or something.

I've tried multiple sites like google and still same result, text off the screen or images off the screen. I was thinking it would shrink after all when I go to the site using Safari on the iPhone it displays properly but through an app/webview I get: Current Webview

Here is my viewcontroller.m:

#import "ViewController.h"
@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad 
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    webPage.delegate = self;
    webPage.scalesPageToFit = NO;
    NSString *website = @"http://www.youtube.com:";
    NSURL *url = [NSURL URLWithString:website];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];

    [webPage loadRequest:request];

}

- (void)webViewDidFinishLoad:(UIWebView *)webView 
{ 
    [webPage     stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"document.querySelector('meta[name=viewport]').setAttribute('content', 'width=%d;', false); ", (int)webPage.frame.size.width]]; 
}

- (void)didReceiveMemoryWarning 
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end

Here is my viewcontroller.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController<UIWebViewDelegate>{
    IBOutlet UIWebView *webPage;   
}
@end
Popeye
  • 11,839
  • 9
  • 58
  • 91
Dobbs
  • 75
  • 1
  • 5

2 Answers2

1

Try this

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    webView.scalesPageToFit = YES;//set here
    return YES;
}
Suyash Seth
  • 85
  • 1
  • 10
0

Ok so for me what worked was to select the UIWebView on my Main.storyboard and click the Resolve Auto Layout Issues button in the lower right hand corner. (Icon is a small triangle with vertical lines on either side....looks kind of like a Tie Fighter from Star Wars...lol) From there I selected "Reset to Suggested Constraints" and ran my app again...everything displayed correctly! Thanks to Agent and Ayan for pointing me in the right direction with constraints!

Dobbs
  • 75
  • 1
  • 5