iOS开发——设备篇Swift篇&判断设备类型

时间:2023-03-05 09:11:14

判断设备类型

1,分割视图控制器(UISplitViewController)
在iPhone应用中,使用导航控制器由上一层界面进入下一层界面。
但iPad屏幕较大,通常使用SplitViewController来实现导航(这个是iPad专用的视图控制器)。在横屏下,左侧显示一个导航列表,点击后右边显示对应的详情。竖屏情况下显示方式会有所不同,默认只显示详细面板,原来左侧的导航列表会通过浮动窗口隐藏,需要从边缘向内拖动来显示。
2,开发兼容的iOS应用
有时候需要开发兼容iPhone、iPod、iPad的应用,这时候需要判断设备类型,如果是iPhone、iPod就不应该使用SplitViewController。另外处理方式也会有变化,如点击列表项时,在iPad直接在右侧展示详情,而iPhone却需要导航到详细页。
iOS提供了UIDevice类来判断设备的类型,其userInterfaceIdiom属性返回设备类型枚举
3,样例效果图
  iPhone:
   iOS开发——设备篇Swift篇&判断设备类型  iOS开发——设备篇Swift篇&判断设备类型
  iPad:
   iOS开发——设备篇Swift篇&判断设备类型

4,样例代码
--- AppDelegate.swift 应用入口 ---

 import UIKit

 @UIApplicationMain
 class AppDelegate: UIResponder, UIApplicationDelegate {

     var window: UIWindow?

     func application(application: UIApplication,
         didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {
         self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
         // Override point for customization after application launch.
         self.window!.backgroundColor = UIColor.whiteColor()
         self.window!.makeKeyAndVisible()

         //初始化列表面板
         let master = MasterViewController()
         //初始化详情面板
         let detail = DetailViewController()
         //设置列表面板引用详情面板,以便用户点击列表项时调用详情面板的相应方法
         master.detailViewController = detail
         //用导航包装master列表,显示导航条,如果是分割面板也不影响功能
         let nav = UINavigationController(rootViewController: master)
         // 如果是iPhone或iPod则只显示列表页,如果是iPad则显示分割面板
24         if (UIDevice.currentDevice().userInterfaceIdiom == .Phone) {
25             self.window!.rootViewController = nav
26         }
27         else {
28             //初始化分割面板
29             let split = UISplitViewController()
30             //设置分割面板的2个视图控制器
31             split.viewControllers = [nav, detail]
32
33             //分割面板作为window的主视图加载
34             self.window!.rootViewController = split
35         }
36
         return true
     }

     func applicationWillResignActive(application: UIApplication) {
     }

     func applicationDidEnterBackground(application: UIApplication) {
     }

     func applicationWillEnterForeground(application: UIApplication) {
     }

     func applicationDidBecomeActive(application: UIApplication) {
     }

     func applicationWillTerminate(application: UIApplication) {
     }
 }

--- MasterViewController.swift 列表页 ---

 import UIKit

 class MasterViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

     // 表格加载
     var tableView:UITableView?
     // 控件类型
     var ctrls = ["UILabel", "UIButton", "UIImageView", "UISlider"]
     //
     var detailViewController:DetailViewController?

     override func viewDidLoad() {
         super.viewDidLoad()
         // Do any additional setup after loading the view, typically from a nib.

         self.title = "Swift控件演示"
         self.tableView = UITableView(frame:self.view.frame, style:UITableViewStyle.Plain)
         self.tableView!.delegate = self
         self.tableView!.dataSource = self
         self.tableView!.registerClass(UITableViewCell.self, forCellReuseIdentifier: "SwiftCell")
         self.view.addSubview(self.tableView!)
     }

     override func didReceiveMemoryWarning() {
         super.didReceiveMemoryWarning()
         // Dispose of any resources that can be recreated.
     }

     // UITableViewDataSource协议方法
     func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
     {
         return self.ctrls.count
     }

     // UITableViewDataSource协议方法
     func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath)
         -> UITableViewCell
     {
         let cell = tableView.dequeueReusableCellWithIdentifier("SwiftCell",
             forIndexPath: indexPath) as UITableViewCell
         cell.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator
         cell.textLabel?.text = self.ctrls[indexPath.row]

         return cell
     }

     // UITableViewDelegate协议方法,点击时调用
     func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!)
     {
         //调用DetailViewController的方法更新详细页
         detailViewController!.loadControl(self.ctrls[indexPath.row])

53         //如果是iPhone、iPod则导航到详情页
54         if (UIDevice.currentDevice().userInterfaceIdiom == .Phone) {
55             // 跳转到detailViewController,取消选中状态
56             //self.tableView!.deselectRowAtIndexPath(indexPath, animated: true)
57
58             // navigationController跳转到detailViewController
59             self.navigationController!.pushViewController(detailViewController!, animated:true)
60         }
     }
 }

--- DetailViewController.swift 详情页 ---

 import UIKit

 class DetailViewController: UIViewController {

     override func viewDidLoad() {
         super.viewDidLoad()
         // Do any additional setup after loading the view, typically from a nib.

         self.view.backgroundColor = UIColor.whiteColor()
         let ctrl = self.title != nil ? self.title! : ""
         loadControl(ctrl)
     }

     override func didReceiveMemoryWarning() {
         super.didReceiveMemoryWarning()
         // Dispose of any resources that can be recreated.
     }

     func loadControl(ctrl:String) {
         clearViews()
         switch (ctrl) {
         case "UILabel":
             var label = UILabel(frame: self.view.bounds)
             label.backgroundColor = UIColor.clearColor()
             label.textAlignment = NSTextAlignment.Center
             label.font = UIFont.systemFontOfSize()
             label.text = "Hello, Hangge.com"
             self.view.addSubview(label)
         case "UIButton":
             var button = UIButton(frame: CGRectMake(,,,))
             button.backgroundColor = UIColor.blueColor()
             button.setTitleColor(UIColor.redColor(), forState: UIControlState.Normal)
             button.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Highlighted)
             button.setTitle("点击我", forState: .Normal)
             self.view.addSubview(button)
         default:
             println("clicked: \(ctrl)")
         }
     }

     func clearViews() {
         for v in self.view.subviews {
             v.removeFromSuperview()
         }
     }
 }

(注意:项目直接新建一个Master-Detail Application,就已经具有同上述一样的兼容iPhone、iPad的二级导航功能)