https://blog.csdn.net/weixin_43704791/article/details/86424080

2019年01月13日

AppDelegate中:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

// Override point for customization after application launch.

let master = MasterViewController()

let detail = DetailViewController()

master.detailViewController = detail

let navigationController = UINavigationController(rootViewController: master)

//判断设备

if UIDevice.current.userInterfaceIdiom == .phone{

self.window?.rootViewController = navigationController

}else{

//创建分割视图控制器(iPad特有)

let split = UISplitViewController()

//横屏下显示

split.viewControllers = [navigationController,detail]

self.window?.rootViewController = split

}

return true

}

创建MAsterViewController:

import UIKit

class MasterViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {

var tableView:UITableView!

var ctrls = ["A","B","C"]

var detailViewController:DetailViewController!

override func viewDidLoad() {

super.viewDidLoad()

self.title = "列表"

self.tableView = UITableView(frame: self.view.frame,style:.plain)

self.tableView.delegate = self

self.tableView.dataSource = self

self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: "ReusedCell")

self.view.addSubview(tableView)

}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

return ctrls.count

}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

let identifier = "ReusedCell"

let cell = tableView.dequeueReusableCell(withIdentifier: identifier, for: indexPath)

cell.accessoryType = .disclosureIndicator

cell.textLabel?.text = self.ctrls[indexPath.row]

return cell

}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

detailViewController!.loadControl(ctrl: self.ctrls[indexPath.row])

//        detailViewController.title = ctrls[indexPath.row]

if (UIDevice.current.userInterfaceIdiom == .phone){

tableView.deselectRow(at: indexPath, animated: true)

self.navigationController?.pushViewController(detailViewController, animated: true)

}

}

}

DetailViewController:

import UIKit

class DetailViewController: UIViewController {

override func viewDidLoad() {

super.viewDidLoad()

self.view.backgroundColor = UIColor.white

let ctrl = self.title != nil ? self.title! : ""

loadControl(ctrl: ctrl)

}

func loadControl(ctrl:String){

clearViews()

switch ctrl {

case "A":

let label = UILabel(frame: self.view.bounds)

label.backgroundColor = UIColor.black

label.textColor = UIColor.orange

label.text = "hellow"

self.view.addSubview(label)

case "B":

let button = UIButton(frame: CGRect(x: 150, y: 250, width: 100, height: 100))

button.setTitle("按钮", for: .normal)

button.backgroundColor = UIColor.red

self.view.addSubview(button)

case "C":

let uiSwitch = UISwitch(frame: CGRect(x: 150, y: 250, width: 0, height: 0))

uiSwitch.setOn(false, animated: true)

self.view.addSubview(uiSwitch)

default:

print("error")

}

}

func clearViews(){

for v in self.view.subviews{

v.removeFromSuperview()

}

}

}

Swift - UISplitViewController的更多相关文章

  1. Swift之分割视图控制器-UISplitViewController

    Swift之分割视图控制器-UISplitViewController UISplitViewController这种控制器只能用于iPad,它可以在iPad屏幕中显示两个不同的场景:在横向模式下,左 ...

  2. iOS - UISplitViewController

    前言 NS_CLASS_AVAILABLE_IOS(3_2) @interface UISplitViewController : UIViewController @available(iOS 3. ...

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

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

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

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

  5. iOS代码规范(OC和Swift)

    下面说下iOS的代码规范问题,如果大家觉得还不错,可以直接用到项目中,有不同意见 可以在下面讨论下. 相信很多人工作中最烦的就是代码不规范,命名不规范,曾经见过一个VC里有3个按钮被命名为button ...

  6. Swift与C#的基础语法比较

    背景: 这两天不小心看了一下Swift的基础语法,感觉既然看了,还是写一下笔记,留个痕迹~ 总体而言,感觉Swift是一种前后端多种语言混合的产物~~~ 做为一名.NET阵营人士,少少多多总喜欢通过对 ...

  7. iOS开发系列--Swift语言

    概述 Swift是苹果2014年推出的全新的编程语言,它继承了C语言.ObjC的特性,且克服了C语言的兼容性问题.Swift发展过程中不仅保留了ObjC很多语法特性,它也借鉴了多种现代化语言的特点,在 ...

  8. 算法与数据结构(十七) 基数排序(Swift 3.0版)

    前面几篇博客我们已经陆陆续续的为大家介绍了7种排序方式,今天博客的主题依然与排序算法相关.今天这篇博客就来聊聊基数排序,基数排序算法是不稳定的排序算法,在排序数字较小的情况下,基数排序算法的效率还是比 ...

  9. 算法与数据结构(十五) 归并排序(Swift 3.0版)

    上篇博客我们主要聊了堆排序的相关内容,本篇博客,我们就来聊一下归并排序的相关内容.归并排序主要用了分治法的思想,在归并排序中,将我们需要排序的数组进行拆分,将其拆分的足够小.当拆分的数组中只有一个元素 ...

随机推荐

  1. 【Unity】鼠标点选物体

        Camera cam;    void Start () { cam =this.GetComponent<Camera>(); } void Update () { )) { T ...

  2. SIAMATIC S7-1200 中通过 Modbus RTU 如何读取地址范围 9999 到 65535 的输入字

    原文地址 说明 除了需要 STEP 7 >= V13 SP1 (TIA Portal) 的软件,还需要 S7-1200 CPU 固件版本 >= V4 (文章编号: 6ES721x-1xx4 ...

  3. Eclipse创建一个普通maven项目详细步骤

    首先找到Eclipse最顶部左边的File,new一个 Maven Project项目 下一步,勾选第二个即可 下一步,选择  maven-archetype-webapp Group Id 写域名倒 ...

  4. Monkey自动化测试命令

    adb shell monkey –p com.baidu.yuedu -s 123  --throttle 2000 --ignore-crashes  --ignore-security-exce ...

  5. JavaScript 字符编码

    JavaScript 字符编码 JavaScript 遵循 Unicode 字符编码规则.Unicode 字符集中每个字符使用 2 个字节来表示,这意味着用户可以使用中文来命名 Java)Script ...

  6. GNS3 模拟免费ARP

    R2 : conf t int f0/0 no shutdown ip add 192.168.1.254 255.255.255.0 end R1 : conf t int f0/0 no shut ...

  7. Day8 - B - Non-Secret Cypher CodeForces - 190D

    Berland starts to seize the initiative on the war with Flatland. To drive the enemy from their nativ ...

  8. 029、Java中的四则运算

    01.代码如下: package TIANPAN; /** * 此处为文档注释 * * @author 田攀 微信382477247 */ public class TestDemo { public ...

  9. JS:递归基础及范例——斐波那契数列 、 杨辉三角

    定义:程序调用自身的编程技巧称为递归.一个过程或函数在其定义或说明中有直接或间接调用自身的一种方法,它通常把一个大型复杂的问题层层转化为一个与原问题相似的规模较小的问题来求解,递归策略只需少量的程序就 ...

  10. 开发者说 | 云+AI赋能心电医疗领域的应用

    以"医工汇聚 智竞心电"为主题的首届中国心电智能大赛自2019年1月1日启动全球招募起,共吸引总计545支来自世界各地的医工结合团队,308支团队近780名选手通过初赛资格审查,经 ...