簡體   English   中英

使用Swift在iPhone應用程序中的上下顛倒方向

[英]Upside down orientation in iPhone app using Swift

我正在開發一個應用程序,並打算在其中使用顛倒方向。 我使用了以下代碼

override func willRotateToInterfaceOrientation(toInterfaceOrientation: UIInterfaceOrientation, duration: NSTimeInterval) {
        if toInterfaceOrientation == UIInterfaceOrientation.PortraitUpsideDown {
            self.shouldAutorotate()
        }
    }

但是它不起作用。 任何幫助!

嘗試將其放在您的plist文件中

<key>UISupportedInterfaceOrientations</key>
<array>
    <string>UIInterfaceOrientationPortrait</string>
    <string>UIInterfaceOrientationPortraitUpsideDown</string>
</array>

並在“ 部署”信息的“ 常規”選項卡上驗證您的目標是否具有此功能:

在此處輸入圖片說明

另一方面,您使用的是導航控制器還是Tab控制器? 如果是這樣,您將需要對導航或選項卡控制器進行子類化,並添加以下兩種方法:

    override func shouldAutorotate() -> Bool {
        return true
    }

    override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
        return UIInterfaceOrientationMask.All
    }

設備方向(“常規”標簽):

[x]肖像

[x]顛倒

[]風景左

[]景觀權

迅捷2

// UINavigationController+UpsideDownOrientation.swift

import UIKit
extension UINavigationController {

    public override func shouldAutorotate() -> Bool {
        return true
    }

    public override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {

        return [.Portrait, .PortraitUpsideDown]
    }
}

將此添加到您的視圖控制器代碼中。

override func supportedInterfaceOrientations() -> Int{
    return Int(UIInterfaceOrientationMask.All.rawValue)
}

將此代碼添加到您的Class或ViewController上。

  override func shouldAutorotate() -> Bool {
  return false 
 }  
  override func supportedInterfaceOrientations() -> Int {
  return UIInterfaceOrientation.Portrait.rawValue
}

將這兩個功能添加到您要設置方向的視圖控制器。 適用於Swift 3.0,因為我現在正在使用它。

在項目的“常規”下,選擇要應用程序支持的“設備方向”。 然后轉到.plist並編輯要支持的方向的“支持的界面方向(iPad)”。

override var shouldAutorotate: Bool {
        return true
    }

override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        return .landscapeRight // or .landscapeLeft or .portrait or .portraitUpsideDown which ever orientation you want the viewcontroller to be

}

1)檢查您的info.plist以獲取支持的界面方向,並根據需要進行設置

2)檢查您的部署信息並選擇您選擇的設備方向

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM