Reputation: 6608
I got a view that is used to show different PDF files in a UIWebView
, this UIWebView
load the PDF file as NSData
like:
webView.loadData(pdfData, MIMEType: "application/pdf", textEncodingName: "utf-8", baseURL: NSURL())
Now there are some PDF files that need to be rotated via UIButton
that is above the UIWebView
, is there a way to rotate the PDF inside of the UIWebView
?
What i did try so far was
webView.transform = CGAffineTransformMakeRotation(CGFloat(M_PI_2)
)
but this was not getting me the result i was looking for.
Upvotes: 5
Views: 1605
Reputation: 35402
If I have understand your problem it's to center your PDF after you have rotate it, to showing it correctly from the top, unfortunately CGAffineTransformMakeRotation
only it's not enough..
You can do:
webView.scalesPageToFit = true
let scale = CGAffineTransformMakeScale(0.6, 0.6)
let translate = CGAffineTransformMakeTranslation(webView.frame.origin.x, webView.frame.origin.y - webView.frame.size.height * 0.25)
let transform = CGAffineTransformConcat(translate, scale);
webView.transform = CGAffineTransformRotate(transform, CGFloat(M_PI_2))
And to return back from transform:
webView.transform = CGAffineTransformIdentity
There is also another working method but I don't recommend to do:
(it's too long and too hard to handle html page just to make a rotation..)
set webView -> load a responsive HTML page -> this responsive HTML page load PDF -> inject to webView the following javascript code
func webViewDidFinishLoad(webView: UIWebView) {
let js = "document.body.style.setProperty('-webkit-transform', 'rotate(-90deg)', null);"
webView.stringByEvaluatingJavaScriptFromString(js)
}
You can check your JS also from JSFiddle if you want to do it.
Upvotes: 3
Reputation: 6608
Currently i do use this code to rotate, but the "view does not stay centered (i think based on my auto layout)
print(webView.transform)
if (webView.transform.a == 1.0 && webView.transform.d == 1.0) {webView.transform = CGAffineTransformMakeRotation(CGFloat(M_PI/2));print("done1")}
else if(webView.transform.b == 1.0 && webView.transform.c == -1.0) {webView.transform = CGAffineTransformMakeRotation(CGFloat(M_PI));print("done2")}
else if(webView.transform.a == -1.0 && webView.transform.d == -1.0) {webView.transform = CGAffineTransformMakeRotation(CGFloat(M_PI+(M_PI/2)));print("done3")}
else if(webView.transform.b == -1.0 && webView.transform.c == 1.0) {webView.transform = CGAffineTransformMakeRotation(CGFloat(M_PI + M_PI));print("done4")}
else{print("nothing happens")}
example vid https://i.sstatic.net/9iCuD.jpg
Upvotes: 1
Reputation: 8516
shouldAutorotate
method.Check the current statusBarOrientation
, then update the UIInterfaceOrientation
as follows:-
let interfaceOrienation : UIInterfaceOrientation = UIApplication.sharedApplication().statusBarOrientation
if(interfaceOrienation == UIInterfaceOrientation.Portrait){
let value = UIInterfaceOrientation.LandscapeLeft.rawValue
UIDevice.currentDevice().setValue(value, forKey: "orientation")
}else{
let value = UIInterfaceOrientation.Portrait.rawValue
UIDevice.currentDevice().setValue(value, forKey: "orientation")
}
I developed a sample project
for you. Please follow the below link to download the full code
.
https://www.dropbox.com/s/3hk5y9g7v74cbkd/Stackoverflow_webview.zip?dl=0
Upvotes: 7