1

I've created UIWebView programmatically. Now i want to set programmatically its orientation to (landscape or portrait). Please can anyone tell me how i can do this? Thanks

import UIKit class ViewController: UIViewController, UIWebViewDelegate { override func viewDidLoad() { super.viewDidLoad() let webV:UIWebView = UIWebView(frame: CGRectMake(0, 0, UIScreen.mainScreen().bounds.width, UIScreen.mainScreen().bounds.height)) webV.loadRequest(NSURLRequest(URL: NSURL(string: "http://tune.tv/humtv-live-embed")!)) webV.delegate = self; self.view.addSubview(webV) } } 
2
  • Visit this link stackoverflow.com/questions/25651969/… Commented May 7, 2016 at 8:41
  • Thanks but i just want to change orientation of UIWebView not of Whole ViewController class. Because i have to use UIWebView in CollectionVIew Class for cells. Commented May 7, 2016 at 10:08

1 Answer 1

5

In your viewDidAppear add the following:

override func viewDidAppear(animated: Bool) { let value = UIInterfaceOrientation.LandscapeRight.rawValue UIDevice.currentDevice().setValue(value, forKey: "orientation") } 

Swift 3.0:

override func viewDidAppear(_ animated: Bool) { let value = UIInterfaceOrientation.landscapeRight.rawValue UIDevice.current.setValue(value, forKey: "orientation") } 

Just choose your orientation and it will be set to it when the WebView is shown.

Sign up to request clarification or add additional context in comments.

5 Comments

I just want to change orientation of UIWebView not of Whole ViewController class. Because i have to use UIWebView in CollectionVIew Class for cells. And when i run first time my project after adding that code in my class it show in landscape but when i change it to portrait by using Command and right arrow. and then again run my project it not show in landscape mode?
I don´t think it´s possible to achieve what you´re trying to do. You can only change the orientation for the entire view as I showed in my example. If you need further help do tell me.
For example in this Project i just want to change orientation of UIWebView to landscape when it loads and when i exit UIWebView then collectionView Show in portrait? drive.google.com/open?id=0B29ka7gbDAYxLTJWVXJTS3FQSFU
Just move the webView to a new viewController and control it that way. I updated your project with that functionality. Download a new version at this link dropbox.com/s/hvoeddliwstzn7q/videoPlay.zip?dl=0
Thank you very much Rashwan L. Now its working as i want. And i replace this line cell.playBt.addTarget(self, action: #selector(ViewController.webView(_:)), forControlEvents: UIControlEvents.TouchUpInside) with this cell.playBt.addTarget(self, action: Selector("webView:"), forControlEvents: UIControlEvents.TouchUpInside) And works (y)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.