Swift
发布时间:2022-03-31 发布网站:大佬教程 code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Swift - 故事板storyboard的用法,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
概述
故事板(UIStoryboard)可以很方
便的进行界面的设计,下面总结了常用的几个操作方法: 1,初始场景 选中View Controller,在属性面板里勾选Is Initial View Controller复选框,即可设置为起始场景(前面会显示灰色的小箭头) 2,将View Controller的尺寸改成iPhone大小 (1)点击左侧的Main.storyboard。然后选择右侧的 sho
故事板(UIStoryboard)可以很方便的进行界面的设计,下面总结了常用的几个操作方法:
1,初始场景
选中View Controller,在
属性面板里勾选Is Initial View Controller复选框,即可设置为起始场景(前面会
显示灰色的小箭头)
2,将View Controller的尺寸改成iPhone大小
(1)点击左侧的Main.storyboard。然后选择右侧的 show the File inspector(纸张图标),最后
将use Size Classes前面的勾去掉。
(2)在弹出的窗口中,选择iPhone为Keep size class data 的值。然后点击Disable Size Class按钮
即可。
(3)此时Xcode中
间的布局样式看上去就像个iPhone的样子了。
使用seque的好处是,
页面的切换不再需要创建任何
代码。按住Ctrl键同时拖动控件到目标场景,在弹出的上下文
菜单中选择show。
(1)
菜单中中间4个是过去版本的使用方式,推荐最上面的4个新的方式:
Show Detail:替换当前的视图方式来展现新的视图
@H_650_52
@modally:模式窗口的方式
Popover:浮窗形式
(2)如果是两个Controller之间建立包含关联,例如从TableBarController到NavigationController,则上下文
菜单会有relationship的选项,选择view controllers即可
在stroyboard中
添加一个seque时并不会同步
添加对应的类。如果需要,我们要手动先创建
一个swift类,比如叫MyView1。然后
进入storyboard,选中对应的segue。在
属性面板中设置对应的Class,如下图:
打开Assistant Editor同时
显示界面设计与
代码,按住Ctrl键同时拖动控件到
代码,在弹出的上下文
菜单中设置需要关联的类型,即可在
代码里
自动生成代码。
这里主要有两种关联类型,一种是Outlet连接,就是在
代码里创建界面元素的成员变量引用。另一种是Action事件,把界面元素的响应事件
方法添加到
代码里来。(如果已经创建了
代码,也可以把控件直接拖放到对应
代码上,这时候就不是插入而是直接建立连接了)
6,同一个storyboard里多个View Controller的引用
如果要在
代码里
调用storyboard里的View Controller,可以设置View Controller的identity。
设置方法是,在stroyboard中选中View Controller,在右侧的identity
属性面板里设置StroyboardID。
比如设置类Main.storyboard里初始View Controller的identity为RootView,则通过以下方式引用:
1
2
|
var
rootViewController = UIStoryboard(name:
"Main"
,bundle: nil)
.instantiateViewControllerWithIdentifier(
"RootView"
)
as
UIViewController
|
对于初始View Controller也可以不通过identity直接获取:
var
rootViewController =
UIStoryboard
(name:
"Main"
:1.5em!important; margin:0px!important; overflow:visible!important; padding:1px 0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,bundle:
nil
)
.instantiateInitialViewController()
as
UIViewController