【问题标题】:device orientation change in uiviewcontroller [duplicate]uiviewcontroller中的设备方向更改[重复]
【发布时间】:2023-03-06 03:31:01
【问题描述】:

我想在设备方向发生变化时在 UIViewcontroller 中捕获事件并相应地更改视图内容。但是,执行此操作的函数似乎已被弃用。如何解决?

【问题讨论】:

    标签: ios swift


    【解决方案1】:

    在“didFinishLaunchingWithOptions”函数内的 AppDelegate.swift 中:

    NSNotificationCenter.defaultCenter().addObserver(self, selector: "rotated", name: UIDeviceOrientationDidChangeNotification, object: nil)
    

    然后在 AppDelegate 类中放入以下函数:

    func rotated()
    {
        if(UIDeviceOrientationIsLandscape(UIDevice.currentDevice().orientation))
        {            
            println("landscape")
        }
    
        if(UIDeviceOrientationIsPortrait(UIDevice.currentDevice().orientation))
        {
            println("Portrait")
        }
    
    }
    

    【讨论】:

    • 我想捕捉视图控制器中的方向变化。我不明白如何用上面的例子来捕捉这个事件。
    • 如果您没有设置 AppDelegate,您可以将 NotificationCenter... 行放在 View Controller 的 viewDidLoad 中,并将 func rotated() 添加为 View Controller 的附加功能
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-20
    相关资源
    最近更新 更多