检测设备方向变化,API变化真快,讨论热烈.
http://stackoverflow.com/questions/25666269/ios8-swift-how-to-detect-orientation-change
起因是在看appcoda的那本iOS 10的书,有个练习是在ipad(portrait)模式下修改某个stack view的spacing从10变成30.
看了一下,好像ipad不管怎么摆size classes都是wR/hR,所以不能通过interface builder修改,只能通过写代码的方式.
最终的我的代码如下(AppDelegate.swift):
func application(_ application: UIApplication,didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { // Override point for customization after application launch. NotificationCenter.default.addObserver(self,selector: #selector(rotated),name: NSNotification.Name.UIDeviceOrientationDidChange,object: nil) return true } func rotated() { let isIPad = UIDevice.current.userInterfaceIdiom == .pad let isPortrait = UIDeviceOrientationIsPortrait(UIDevice.current.orientation) print("isIPad: \(isIPad),iPortrait: \(isPortrait)") if isIPad { let controller = window!.rootViewController as! ViewController controller.myStackView.spacing = isPortrait ? 30.0 : 10.0 } }
参考:
- http://stackoverflow.com/questions/25666269/ios8-swift-how-to-detect-orientation-change
- iOS Apprentice 5th edition
- Beginning iOS programming with Swift
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。