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

前端之家收集整理的这篇文章主要介绍了Swift - 判断设备类型开发兼容的iOS应用(iPad使用分隔视图控制器)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
1,分割视图控制器(UISplitViewController)
在iPhone应用中,使用导航控制器由上一层界面进入下一层界面。
但iPad屏幕较大,通常使用SplitViewController来实现导航(这个是iPad专用的视图控制器)。在横屏下,左侧显示一个导航列表,点击后右边显示对应的详情。竖屏情况下显示方式会有所不同,默认只显示详细面板,原来左侧的导航列表会通过浮动窗口隐藏,需要从边缘向内拖动来显示

2,开发兼容的iOS应用
有时候需要开发兼容iPhone、iPod、iPad的应用,这时候需要判断设备类型,如果是iPhone、iPod就不应该使用SplitViewController。另外处理方式也会有变化,如点击列表项时,在iPad直接在右侧展示详情,而iPhone却需要导航到详细页。
iOS提供了UIDevice类来判断设备的类型,其userInterfaceIdiom属性返回设备类型枚举
3,样例效果
iPhone:
iPad:(注:iPad要旋转成横屏,竖屏下一片空白)


4,样例代码
--- 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
55
import UIKit
@UIApplicationMain
class AppDelegate : UIResponder , UIApplicationDelegate {
var window: UIWindow ?
func application(application: UIApplication didFinishLaunchingWithOptions launchOptions: [ NSObject : AnyObject ]?) -> Bool {
self .window = (frame: UIScreen .mainScreen().bounds)
// Override point for customization after application launch.
.window!.backgroundColor = UIColor .whiteColor()
.window!.makeKeyAndVisible()
//初始化列表面板
let master = MasterViewController ()
//初始化详情面板
detail = DetailViewController ()
//设置列表面板引用详情面板,以便用户点击列表项时调用详情面板的相应方法
master.detailViewController = detail
//用导航包装master列表,显示导航条,如果是分割面板也不影响功能
nav = UINavigationController (rootViewController: master)
// 如果是iPhone或iPod则只显示列表页,如果是iPad则显示分割面板
if ( UIDevice .currentDevice().userInterfaceIdiom == . Phone ) {
.window!.rootViewController = nav
}
else {
//初始化分割面板
split = UISplitViewController ()
//设置分割面板的2个视图控制器
split.viewControllers = [nav,detail]
//分割面板作为window的主视图加载
.window!.rootViewController = split
}
return true
}
applicationWillResignActive(application: ) {
}
applicationDidEnterBackground(application: ) {
}
applicationWillEnterForeground(application: ) {
}
@H_533_403@
applicationDidBecomeActive(application: ) {
}
applicationWillTerminate(application: ) {
}
}

--- MasterViewController.swift 列表页 ---
24
25
26
27
28
29
30
31
32
33
34
35
36
48
49
50
51
52
53
54
55
56
57
58
59
60
61
UIViewController UITableViewDelegate UITableViewDataSource { @H_128_502@
// 表格加载
tableView: UITableView // 控件类型
ctrls = [ "UILabel" "UIButton" "UIImageView" "UiSlider" ]
//
detailViewController: ?
override viewDidLoad() {
super .viewDidLoad()
.title = "Swift控件演示"
.tableView = (frame: .view.frame,style: UITableViewStyle . Plain )
.tableView!.delegate = self
.tableView!.dataSource = self
.tableView!.registerClass( UITableViewCell . "SwiftCell" )
.view.addSubview( .tableView!)
}
didReceiveMemoryWarning() {
.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// UITableViewDataSource协议方法
tableView(tableView: Int ) -> Int
{
.ctrls.count
}
// UITableViewDataSource协议方法
NSIndexPath )
-> UITableViewCell
{
cell = tableView.dequeueReusableCellWithIdentifier( forIndexPath: indexPath)
cell.accessoryType = UITableViewCellAccessoryType DisclosureIndicator
cell.textLabel?.text = .ctrls[indexPath.row]
return cell
}
// UITableViewDelegate协议方法,点击时调用
)
{
//调用DetailViewController的方法更新详细页
detailViewController!.loadControl( .ctrls[indexPath.row])
//如果是iPhone、iPod则导航到详情页
) {
// 跳转到detailViewController,取消选中状态
//self.tableView!.deselectRowAtIndexPath(indexPath,animated: true)
// navigationController跳转到detailViewController
.navigationController!.pushViewController(detailViewController!,animated: true )
}
}
--- DetailViewController.swift 详情页 ---
44
viewDidLoad() {
.viewDidLoad()
.view.backgroundColor = .whiteColor()
ctrl = .title != nil ? .title! : ""
loadControl(ctrl)
}
didReceiveMemoryWarning() {
.didReceiveMemoryWarning()
}
loadControl(ctrl: String ) {
clearViews()
switch (ctrl) {
case :
label = UILabel .view.bounds)
label.backgroundColor = .clearColor()
label.textAlignment = NSTextAlignment Center
label.font = UIFont .systemFontOfSize(36)
label.text = "Hello,Hangge.com"
.view.addSubview(label)
:
button = UIButton CGRectMake (110,120,100,60))
button.backgroundColor = .blueColor()
button.setTitleColor( .redColor(),forState: UIControlState Normal )
.whiteColor(),147)!important">Highlighted )
button.setTitle( "点击我" )
.view.addSubview(button)
default :
print ( "clicked: \(ctrl)" }
clearViews() {
for v in .view.subviews {
v.removeFromSuperview()
}
}
(注意:项目直接新建一个Master-Detail Application,就已经具有同上述一样的兼容iPhone、iPad的二级导航功能
原文出自: www.hangge.com 转载请保留原文链接 http://www.hangge.com/blog/cache/detail_636.html 原文链接:https://www.f2er.com/swift/324643.html

猜你在找的Swift相关文章