第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在SWIFT中將單視圖控制器的方向鎖定為縱向模式

如何在SWIFT中將單視圖控制器的方向鎖定為縱向模式

天涯盡頭無女友 2019-10-16 12:09:08
如何在SWIFT中將單視圖控制器的方向鎖定為縱向模式因為我的應(yīng)用程序得到了所有方向的支持。我只想鎖定特定UIViewController的縱向模式。(例如,假設(shè)它是選項卡式應(yīng)用程序,并且當(dāng)signin View以模態(tài)方式出現(xiàn)時,我只希望該signin View只有在用戶如何旋轉(zhuǎn)設(shè)備或當(dāng)前設(shè)備方向的情況下才能進(jìn)入縱向模式)
查看完整描述

3 回答

?
胡說叔叔

TA貢獻(xiàn)1804條經(jīng)驗 獲得超8個贊

當(dāng)您有一個復(fù)雜的視圖層次結(jié)構(gòu)時,事情會變得非?;靵y,比如有多個導(dǎo)航控制器和/或選項卡視圖控制器。

這個實現(xiàn)讓各個視圖控制器在希望鎖定方向時設(shè)置它,而不是依賴AppDelegate通過迭代子視圖來找到它們。

SWIFT 3&4

在附錄中:

/// set orientations you want to be allowed in this property by defaultvar orientationLock = UIInterfaceOrientationMask.allfunc a
pplication(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
        return self.orientationLock}

在其他一些全局struct或Helper類中,我在這里創(chuàng)建了AppUtility:

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")
        UINavigationController.attemptRotationToDeviceOrientation()
    }}

然后,在所需的ViewController中,您要鎖定方向:

 override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

    AppUtility.lockOrientation(.portrait)
    // Or to rotate and lock    // AppUtility.lockOrientation(.portrait, andRotateTo: .
    portrait)}override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)

    // Don't forget to reset when view is being removed    AppUtility.lockOrientation(.all)}

如果iPad或通用應(yīng)用程序

確保在目標(biāo)設(shè)置->General->DeploymentInfo中簽入“Required全屏”。supportedInterfaceOrientationsFor如果不選中委托,則不會調(diào)用該委托。



查看完整回答
反對 回復(fù) 2019-10-17
?
SMILET

TA貢獻(xiàn)1796條經(jīng)驗 獲得超4個贊

SWIFT 4

var orientationLock = UIInterfaceOrientationMask.allfunc application(_ application: UIApplication, supportedInterfaceOrientationsFor window: 
UIWindow?) -> UIInterfaceOrientationMask {
    return self.orientationLock}struct AppUtility {
    static func lockOrientation(_ orientation: UIInterfaceOrientationMask) {
        if let delegate = UIApplication.shared.delegate as? AppDelegate {
            delegate.orientationLock = orientation        }
    }

    static func lockOrientation(_ orientation: UIInterfaceOrientationMask, andRotateTo rotateOrientation:UIInterfaceOrientation) {
        self.lockOrientation(orientation)
        UIDevice.current.setValue(rotateOrientation.rawValue, forKey: "orientation")
    }}

您的視圖控制器如果您只需要縱向方向,請?zhí)砑右韵滦小D仨殞⒋藨?yīng)用于所有ViewController需要顯示的縱向模式。

override func viewWillAppear(_ animated: Bool) {AppDelegate.AppUtility.lockOrientation(UIInterfaceOrientationMask.portrait, andRotateTo:
 UIInterfaceOrientation.portrait)
    }

這將使屏幕定位為其他視圖控制器根據(jù)設(shè)備的物理方向。

override func viewWillDisappear(_ animated: Bool) {
        AppDelegate.AppUtility.lockOrientation(UIInterfaceOrientationMask.all)

    }



查看完整回答
反對 回復(fù) 2019-10-17
?
弒天下

TA貢獻(xiàn)1818條經(jīng)驗 獲得超8個贊

添加此代碼以強(qiáng)制顯示并鎖定它:

override func viewDidLoad() {
    super.viewDidLoad()

    // Force the device in portrait mode when the view controller gets loaded    UIDevice.currentDevice().
    setValue(UIInterfaceOrientation.Portrait.rawValue, forKey: "orientation") }override func shouldAutorotate() -> Bool {
    // Lock autorotate    return false}override func supportedInterfaceOrientations() -> Int {

    // Only allow Portrait    return Int(UIInterfaceOrientationMask.Portrait.rawValue)}
    override func preferredInterfaceOrientationForPresentation() -> UIInterfaceOrientation {

    // Only allow Portrait    return UIInterfaceOrientation.Portrait}

在AppDelegate-set Support InterfaceOrientationsForWindow中,您希望整個應(yīng)用程序支持任何方向:

func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> UIInterfaceOrientationMask {
    return UIInterfaceOrientationMask.All}



查看完整回答
反對 回復(fù) 2019-10-17
  • 3 回答
  • 0 關(guān)注
  • 479 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號