Swift - 判断设备类型开发兼容的iOS应用(iPad使用分隔视图控制器)
1,分割视图控制器(UISplitViewController)

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


iPad:(注:iPad要旋转成横屏,竖屏下一片空白)

--- AppDelegate.swift 应用入口 ---
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
import UIKit @UIApplicationMain class AppDelegate : UIResponder , UIApplicationDelegate { var window: UIWindow ? func application(application: UIApplication , didFinishLaunchingWithOptions launchOptions: [ NSObject : AnyObject ]?) -> 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则显示分割面板 if ( UIDevice .currentDevice().userInterfaceIdiom == . Phone ) { self .window!.rootViewController = nav } else { //初始化分割面板 let split = UISplitViewController () //设置分割面板的2个视图控制器 split.viewControllers = [nav, detail] //分割面板作为window的主视图加载 self .window!.rootViewController = split } return true } func applicationWillResignActive(application: UIApplication ) { } func applicationDidEnterBackground(application: UIApplication ) { } func applicationWillEnterForeground(application: UIApplication ) { } func applicationDidBecomeActive(application: UIApplication ) { } func applicationWillTerminate(application: UIApplication ) { } } |
--- MasterViewController.swift 列表页 ---
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
|
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]) //如果是iPhone、iPod则导航到详情页 if ( UIDevice .currentDevice().userInterfaceIdiom == . Phone ) { // 跳转到detailViewController,取消选中状态 //self.tableView!.deselectRowAtIndexPath(indexPath, animated: true) // navigationController跳转到detailViewController self .navigationController!.pushViewController(detailViewController!, animated: true ) } } } |
--- DetailViewController.swift 详情页 ---
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
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(36) label.text = "Hello, Hangge.com" self .view.addSubview(label) case "UIButton" : var button = UIButton (frame: CGRectMake (110,120,100,60)) 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() } } } |
分类:
Swift语言
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
2014-09-25 Android 出现警告Exported service does not require permission
2013-09-25 Android中的JSON详细总结