ios - WKWebView displays content wrong after orientation change -
i new ios development , i'm using wkwebview
display page plays video using vimeo player. when app starts up, shows view correctly, no matter orientation in. however, once change orientation view either zoomed in or has whitespace below video.
the annoying thing it's not consistent. view displays correctly, doesn't. if try zoom view in or out or try scroll when using app corrects itself, not seem 100% reliable.
some screenshots (tested on ipad 2):
landscape orientation (correct):
portrait orientation (correct):
landscape orientation (incorrect):
portrait orientation (incorrect):
and code used produce result:
import foundation import uikit import webkit class videoviewcontroller : uiviewcontroller, wkscriptmessagehandler { @iboutlet var containerview : uiview! = nil var webview: wkwebview? override func loadview() { super.loadview() self.webview = wkwebview() let contentcontroller = wkusercontentcontroller(); let scaletofit = wkuserscript(source: "var meta = document.createelement('meta'); meta.setattribute('name', 'viewport'); meta.setattribute('content', 'width=device-width, initial-scale=1.0'); document.getelementsbytagname('head')[0].appendchild(meta);", injectiontime: wkuserscriptinjectiontime.atdocumentstart, formainframeonly: true) contentcontroller.adduserscript(scaletofit) contentcontroller.addscriptmessagehandler(self, name: "callbackhandler") self.view = webview! } override func viewdidload() { super.viewdidload() refreshui() } override func didreceivememorywarning() { super.didreceivememorywarning() // dispose of resources can recreated. } func refreshui() { let url = nsurl(string: "https://photofactsacademy.nl/api/vp.asp?id=152839850") let requestobj = nsurlrequest(url: url!) webview!.loadrequest(requestobj) } func usercontentcontroller(usercontentcontroller: wkusercontentcontroller, didreceivescriptmessage message: wkscriptmessage) { if(message.name == "callbackhandler") { print("javascript sending message \(message.body)") } } }
i looked through wkwebview , orientation change , didn't find helped me.
your appreciated.
i able solve problem injecting javascript reload call this:
swift:
func viewwilltransitiontosize(size: cgsize, withtransitioncoordinator coordinator: uiviewcontrollertransitioncoordinator) { coordinator.animatealongsidetransition(nil, completion: { //reset frame of webview webview.evaluatejavascript("location.reload();", completionhandler: nil) }
objective c:
-(void)viewwilltransitiontosize:(cgsize)size withtransitioncoordinator:(id<uiviewcontrollertransitioncoordinator>)coordinator { //reset frame of webview [_webview evaluatejavascript:@"location.reload();" completionhandler:^(id _nullable obj, nserror * _nullable error) { nslog(@"error:%@", error); }]; }
Comments
Post a Comment