簡體   English   中英

鎖定方向在 ipad ios 8 swift xcode 6.2 上不起作用

[英]locking orientation does not work on ipad ios 8 swift xcode 6.2

我將應用程序方向從這樣的常規應用程序設置鎖定為縱向

肖像

但是當我在 ipad 上運行我的應用程序時,它仍然會旋轉!?

那么我應該在哪里鎖定方向?

為什么蘋果在設置中添加了這個功能,但它不起作用!?

更新:

我也嘗試將這個 viewController 實現為所有 viewController 的通用。

import Foundation
import UIKit

class GeneralViewController: UIViewController {

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

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


    override func preferredInterfaceOrientationForPresentation() -> UIInterfaceOrientation {
        return UIInterfaceOrientation.Portrait
    }

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

這張圖片取自我的應用程序的主導航控制器。

答案1的圖片

還是不行!

這東西到底是怎么工作的?

您不需要額外的代碼,我正在使用 Xcode 7.0.1,我注意到當我將屏幕方向鎖定為僅縱向時,它僅在 iPhone 中鎖定,在 iPad 中也將其鎖定,您需要從設備中選擇 iPad (而不是通用)並檢查肖像並取消選中其他人,然后再次將設備返回到通用,因為我發現通用只反映在 iPhone 中,它可能是 Xcode 中的某種錯誤。

我也有同樣的問題。 對我有用的是 -> 在設備類型中選擇 iPad 而不是通用 -> 取消選中除縱向以外的其他三個選項 -> 再次選擇通用。 完畢。

您可以覆蓋您的視圖控制器屬性shouldAutorotate

override var shouldAutorotate: Bool {
    return false 
}

如果您的視圖控制器嵌入在導航控制器中,您可以創建一個自定義導航控制器:

class PortraitNavigationController: UINavigationController {

    override var shouldAutorotate: Bool {
        return false 
    }
    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */
}

這將是添加到 Mohammed Elrashidy 解決方案的評論,但我沒有發表評論的聲譽。 我正在使用 XCode 9.1 和 Swift 3。

我的應用程序需要一些圖形屏幕自動旋轉,但所有其他屏幕保持固定。 我的問題是“常規”>“部署信息”中的“iPad”設備配置與 iPhone 和通用設備的配置不一致。 我為 iPhone 和 Universal 檢查了“Portrait”、“Landscape Left”和“Landscape Right”,但檢查了 iPad 的所有方向(包括“顛倒”)。 簡單地取消選中“顛倒”會導致 iPad 設備在我的 Swift 邏輯中接受 shouldAutorotate 和 supportedInterfaceOrientations 調用。

我的 Info.plist 現在如下所示,我可以以編程方式打開或關閉 iPad 的自動旋轉。

<key>UISupportedInterfaceOrientations</key>
<array>
    <string>UIInterfaceOrientationPortrait</string>
    <string>UIInterfaceOrientationLandscapeLeft</string>
    <string>UIInterfaceOrientationLandscapeRight</string>
</array>
<key>UISupportedInterfaceOrientations~ipad</key>
<array>
    <string>UIInterfaceOrientationPortrait</string>
    <string>UIInterfaceOrientationLandscapeLeft</string>
    <string>UIInterfaceOrientationLandscapeRight</string>
</array>

在按照上述解決方案解決問題后,我只想添加您需要做的其他事情:

在 XCode 中,確保您還選中了 General > Targets 下的“Requires Full Screen”復選框。

參考: iPad 多任務支持需要這些方向

由於 XCode 在這 5 年中發生了很大變化,當我遇到同樣的問題時,我找到了一種簡單的方法來處理它,即使我將設備方向配置為僅允許“縱向”,但該限制在 iPad 設備中不起作用。

所以在新創建的項目中,需要去Targets->Info->Custom iOS Target Properties-> Supported interface orientations(iPad) 然后刪除您不需要的布局,然后這個技巧就會起作用。

打開 Info.plist,在“支持的界面方向 (iPad)”下刪除不需要的項目。

暫無
暫無

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

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