2016-10-25 5 views
0

swift2をコーディングするときに向きを修正する方法を知っていましたが、機能を 'var'に変更した後は新しいコードが機能しません。ここ"Swift3"で向きをプログラマブルに設定する

は、私はこのコードを実行すると、それだけでそのオーバーライドを無視しているようだチェック

class QuizVC: UIViewController{ 

override func viewDidLoad() { 
    super.viewDidLoad() 

    //Quiz scene 
    self.view.backgroundColor = UIColor(red: 240/255, green: 238/255, blue: 226/255, alpha: 1)//sand fron hu 

    let testBtn: UIButton = UIButton()//for orientation test 
    testBtn.setTitle("\" OrientationCheck \"", for: .normal) 
    testBtn.sizeToFit() 
    testBtn.center = self.view.center 
    testBtn.titleLabel?.textColor = UIColor.white 
    testBtn.backgroundColor = UIColor.brown 
    self.view.addSubview(testBtn) 
    //Do any additional setup after loading the view, typically from a nib. 
} 

override var supportedInterfaceOrientations : UIInterfaceOrientationMask {//not working 
    return UIInterfaceOrientationMask.landscape 
} 
override var shouldAutorotate: Bool {//not working 
    return false 
} 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
}} 

用サンプルのUIViewControllerクラスです。 ビューが自動的に回転し、ボタンとラベルが横向きに表示されません。そして固定さえしていない。

私はまた、変数に直接方向値を入れようとしましたが、 'supportedInterfaceOrientations'はget-onlyプロパティです。 もちろん、古いコードは 'supportedInterfaceOrientations()'という関数がなくなるため、エラーとして受け入れられます。変数のみが存在します。

Swift3でこれを試した人はいますか?

私はiOS10でswift3を使用しています。

+0

可能性のある重複した[ポートレートモードでのViewControllerをロックする方法](http://stackoverflow.com/questions/34489764/how-to-lock-ポートレートモードでビューコントローラ) –

答えて

0
  1. これが正常に動作する必要があります(アプリの設定ページ)/一般/デ​​バイスの向きに

を景観を有効shouldAutorotate

  • を削除します。

  • 0
    struct AppUtility { 
    
        static func lockOrientation(_ orientation: UIInterfaceOrientationMask) { 
    
        if let delegate = UIApplication.shared.delegate as? AppDelegate { 
         delegate.orientationLock = orientation 
        } 
        } 
    
        /// OPTIONAL Added method to adjust lock and rotate to the desired orientation 
        static func lockOrientation(_ orientation: UIInterfaceOrientationMask, andRotateTo rotateOrientation:UIInterfaceOrientation) { 
    
        self.lockOrientation(orientation) 
    
        UIDevice.current.setValue(rotateOrientation.rawValue, forKey: "orientation") 
        } 
    
    } 
    

    上記のクラスをプロジェクトに追加します。

    AppUtility.lockOrientation(.landscape、andRotateTo:.portrait)の

    関連する問題