加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Swift3检测设备是否为iPad Portrait模式

发布时间:2020-12-14 06:39:12 所属栏目:百科 来源:网络整理
导读:检测设备方向变化,API变化真快,讨论热烈. http://stackoverflow.com/questions/25666269/ios8-swift-how-to-detect-orientation-change 起因是在看appcoda的那本iOS 10的书,有个练习是在ipad(portrait)模式下修改某个stack view的spacing从10变成30. 看了一

检测设备方向变化,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
   }
}

参考:

  1. http://stackoverflow.com/questions/25666269/ios8-swift-how-to-detect-orientation-change
  2. iOS Apprentice 5th edition
  3. Beginning iOS programming with Swift

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读