【发布时间】:2023-03-06 03:31:01
【问题描述】:
我想在设备方向发生变化时在 UIViewcontroller 中捕获事件并相应地更改视图内容。但是,执行此操作的函数似乎已被弃用。如何解决?
【问题讨论】:
我想在设备方向发生变化时在 UIViewcontroller 中捕获事件并相应地更改视图内容。但是,执行此操作的函数似乎已被弃用。如何解决?
【问题讨论】:
在“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")
}
}
【讨论】:
NotificationCenter... 行放在 View Controller 的 viewDidLoad 中,并将 func rotated() 添加为 View Controller 的附加功能