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
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()
        }
    }   
}

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

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

  1. 学习方法和阶段介绍 、 iOS界面开发引入 、 构造第一个App 、 视图控制器和视图 、 控件与事件 、 InterfaceBuilder

    1 创建并运行第一个App 1.1 问题 使用Xcode创建一个App项目,该应用实现功能在界面上显示Hello World标签,在模拟器中的运行结果如图-1所示: 图-1 1.2 方案 分析图-1, ...

  2. iOS 7 新特性:视图控制器切换API

    本文转载至 http://blog.jobbole.com/51588/ 本文由 伯乐在线 - studentdeng 翻译自 Chris Eidhof.欢迎加入技术翻译小组.转载请参见文章末尾处的要 ...

  3. iOS模态弹出半透明视图控制器

    项目中需要实现点击按钮出现的视图全屏覆盖,呈半透明状态可以看到下面的视图? 解决方案: 绕了很多弯路原来可以使用模态弹出一个视图控制器 在iOS8之后只需要设置一个最新的属性 SecondViewCo ...

  4. IOS中在自定义控件(非视图控制器)的视图跳转中 代理方法与代码块的比较

    //代码块与代替代理的设计方法 我就以在自定义视图中(非视图控制器,不能实现视图控制功能),通过代理和代码块两种方法分别实现视图的跳转,进行对比 首先自定义了一个视图,上面有一个已经注册了得BUtto ...

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

    判断设备类型   1,分割视图控制器(UISplitViewController) 在iPhone应用中,使用导航控制器由上一层界面进入下一层界面. 但iPad屏幕较大,通常使用SplitViewCo ...

  6. iOS开发 - 兼容iOS 10

    1.Notification(通知) 自从Notification被引入之后,苹果就不断的更新优化,但这些更新优化只是小打小闹,直至现在iOS 10开始真正的进行大改重构,这让开发者也体会到UserN ...

  7. iOS开发之Xcode8兼容适配iOS 10资料整理笔记

    1.Notification(通知) 自从Notification被引入之后,苹果就不断的更新优化,但这些更新优化只是小打小闹,直至现在iOS 10开始真正的进行大改重构,这让开发者也体会到UserN ...

  8. Swift - iOS中各种视图控制器(View Controller)的介绍

    在iOS中,不同的视图控制器负责不同的功能,采用不同的风格向用户呈现信息.下面对各个视图控制器做个总结: 1,标准视图控制器 - View Controller 这个控制器只是用来呈现内容.通常会用来 ...

  9. iOS 视图控制器转场详解

    iOS 视图控制器转场详解 前言的前言 唐巧前辈在微信公众号「iOSDevTips」以及其博客上推送了我的文章后,我的 Github 各项指标有了大幅度的增长,多谢唐巧前辈的推荐.有些人问我相关的问题 ...

随机推荐

  1. 工作随记 warning: 'includeantruntime' was not set, defaulting to build.sysclasspath=last; set to false for repeatable builds

    错误信息:F:\BUILD\IDS7020\trunk\manage_src\dev\java_src\tds7030-web\Ant\build.xml:344: warning: 'include ...

  2. Eclipse 和 MyEclipse控制台console不停的自动跳动,跳出来解决方案

    有时候Eclipse启动,控制台console不会自动跳出来,需要手工点击该选项卡才行,按下面的设置,可以让它自动跳出来(或不跳出来):由二种方法: 一.windows  ->   prefer ...

  3. 关于 Swift

    摘自:http://numbbbbb.gitbooks.io/-the-swift-programming-language-/chapter1/01_swift.html Swift 是一种新的编程 ...

  4. windows phone 8的新特性

    <1>硬件的升级WP8在硬件上有了极大的提升,处理器支持双核或多核 理论最大支持64核,分辨率支持800x480.1280x720/768,屏幕支持720p或WXGA:支持存储卡扩展.同时 ...

  5. android-studio 安装gradle

    http://services.gradle.org/distributions 下载需要的gradle 放到C:\Users\Administrator\.gradle\wrapper\dists\ ...

  6. [C++Boost]程序参数项解析库Program_options使用指南

    介绍 程序参数项(program options)是一系列name=value对,program_options 允许程序开发者获得通过命令行(command line)和配置文件(config fi ...

  7. Codeforces Round #258 (Div. 2) 小结

    A. Game With Sticks (451A) 水题一道,事实上无论你选取哪一个交叉点,结果都是行数列数都减一,那如今就是谁先减到行.列有一个为0,那么谁就赢了.因为Akshat先选,因此假设行 ...

  8. 【linux】arm mm内存管理

    欢迎转载,转载时请保留作者信息,谢谢. 邮箱:tangzhongp@163.com 博客园地址:http://www.cnblogs.com/embedded-tzp Csdn博客地址:http:// ...

  9. B4A的软件下载

    http://pan.baidu.com/share/home?uk=909467506#category/type=0

  10. JS Call()与Apply()

    JS Call()与Apply() ECMAScript规范给所有函数都定义了Call()与apply()两个方法,call与apply的第一个参数都是需要调用的函数对象,在函数体内这个参数就是thi ...