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)
    }
 }
Rashwan L
  • 38,237
  • 7
  • 103
  • 107
ZAFAR007
  • 3,049
  • 1
  • 34
  • 45
  • Visit this link http://stackoverflow.com/questions/25651969/setting-device-orientation-in-swift-ios – Jigar Tarsariya May 07 '16 at 08: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. – ZAFAR007 May 07 '16 at 10:08

1 Answers1

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.

Rashwan L
  • 38,237
  • 7
  • 103
  • 107
  • 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? – ZAFAR007 May 07 '16 at 19:09
  • 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. – Rashwan L May 08 '16 at 06:18
  • 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? https://drive.google.com/open?id=0B29ka7gbDAYxLTJWVXJTS3FQSFU – ZAFAR007 May 09 '16 at 16:10
  • 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 https://www.dropbox.com/s/hvoeddliwstzn7q/videoPlay.zip?dl=0 – Rashwan L May 09 '16 at 20:47
  • 1
    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) – ZAFAR007 May 10 '16 at 07:43