UIWebView - doesn't resize content when rotation changes?

So, I have UIWebViewone that displays static html content.

When I start ViewControllerin a portrait and switch to landscape, it resizes the content correctly.

But when I launch the page in landscape orientation and switch to the portrait, it does not change the size of my content, and scrolling is required to view all the content.

This is mistake? Is there a solution to force resize content UIWebView?

+5
source share
4 answers

You have 2 options:
Add this to the section of HEADyour html file:

<meta name="viewport" content="width=device-width" />

Or call [myWebView reload]when orientation changes

+8

. , javascript, , UIViewController:

- (void) didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
    [webView stringByEvaluatingJavaScriptFromString:@"var e = document.createEvent('Events'); "
                                                    @"e.initEvent('orientationchange', true, false);"
                                                    @"document.dispatchEvent(e); "];
}
+3

, :

myWebView.scalesPageToFit = YES;

, , " ", , " ".

, .

+1

@Sergey Kuryanov, . UIWebView loadHTMLString, , , , UIWebView.

, , - . @clearwater82 : UIView
Swift 3, .

, UIWebView loadHTMLString. , UIWebView , HTML . " ". , , :

// Handle rotation
UIDevice.current.beginGeneratingDeviceOrientationNotifications()
NotificationCenter.default.addObserver(
    self,
    selector: #selector(self.orientationChanged(notification:)),
    name: NSNotification.Name.UIDeviceOrientationDidChange,
    object: nil
)

// Called when device orientation changes
func orientationChanged(notification: Notification) {
    // handle rotation here
    self.webView.loadHTMLString(self.htmlText, baseURL: nil)
}

deinit {
    NotificationCenter.default.removeObserver(self)
    UIDevice.current.endGeneratingDeviceOrientationNotifications()
}

:

  • self.htmlText is a variable containing the HTML text I want to load, which I added to my custom view.
  • use UIDevice.current.endGeneratingDeviceOrientationNotifications()was appropriate for my situation, maybe this is not for your

That's all greetings

0
source

All Articles