简体   繁体   中英

iOS UIWebView not scaling content

I have a UIWebView that is always smaller than the device's screen. Whatever I tried, it just won't scale the website to fit its bounds.

Here is my code:

    self.webView = [[UIWebView alloc] initWithFrame:
                    CGRectMake(.08 * width, .18 * height, .84 * width, .74 * height)];
    self.webView.delegate = self;
    self.webView.scrollView.delegate = self;
    self.webView.scrollView.scrollEnabled = NO;
    self.webView.scalesPageToFit = YES;
    NSString *urlAddress = @"https://number26.de/app-tutorial/dist/new-feature-invite.html";
    NSURL *url = [NSURL URLWithString:urlAddress];
    NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
    [self.webView loadRequest:requestObj];
    [self.view.window addSubview:self.webView];

That is how the result looks like: 在此处输入图片说明

And this is how it SHOULD look like (Google Chrome / Dev mode): 在此处输入图片说明

okey, this is my code

- (void)viewDidLoad {
       [super viewDidLoad];



       self.webView = [[UIWebView alloc] init];
       self.webView.frame = CGRectMake(.08 * self.view.frame.size.width, .18 *         self.view.frame.size.height, .84 * self.view.frame.size.width, .74 * self.view.frame.size.height);

      self.webView.delegate = self;
      self.webView.scrollView.delegate = self;
      self.webView.scrollView.scrollEnabled = NO;
      NSString *urlAddress = @"https://s.number26.de/app-tutorial/dist/new-feature-invite.html";
      NSURL *url = [NSURL URLWithString:urlAddress];
      NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];

      [self.webView loadRequest:requestObj];
      [self.view addSubview:self.webView];


      self.webView.scalesPageToFit = YES;

}


 -(void)webViewDidFinishLoad:(UIWebView *)webView {
     [self scaleToFit];
 }

-(void)scaleToFit
{
    if ([self.webView respondsToSelector:@selector(scrollView)])
    {
         UIScrollView *scroll=[self.webView  scrollView];

         float zoom=self.webView .bounds.size.width/scroll.contentSize.width;
         [scroll setZoomScale:zoom animated:YES];
    }
}

and see full website in webView. ]

add this code

-(void)webViewDidFinishLoad:(UIWebView *)webView {
       [self scaleToFit];
}

-(void)scaleToFit
{
       if ([self.webView respondsToSelector:@selector(scrollView)])
       {
              UIScrollView *scroll=[self.webView  scrollView];

              float zoom=self.webView .bounds.size.width/scroll.contentSize.width;
              [scroll setZoomScale:zoom animated:YES];
       }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM