iOS 是由苹果公司开发的移动操做系统 。苹果公司最先于 2007 年 1 月 9 日的 Macworld 大会上公布这个系统(最初叫 iPhone runs OS X)。该系统最初是设计给 iPhone 使用的(因此后来曾命名为 iPhone OS),以后陆续套用到 iPod touch、iPad 以及 Apple TV 等产品上(因此在 WWDC 2010 上最终宣布改名为 iOS)。swift
2007-2020 每一年发布一个新版本,最新版本 iOS 14。数组
iOS 使用 Xcode 工具进行开发。能够在 App Store 搜索安装,也能够去 Apple 开发者网站下载安装(本教程基于 Xcode 12)。安全
Swift 或者 Objective-C(本教程基于 Swift 5.x)。markdown
##App初始化流程网络
@main
(iOS 14 之前是 @UIApplicationMain)。didFinishLaunchingWithOptions
启动方法。SceneDelegate
。class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow?
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
guard let _ = (scene as? UIWindowScene) else { return }
}
func sceneDidDisconnect(_ scene: UIScene) {
}
func sceneDidBecomeActive(_ scene: UIScene) {
}
func sceneWillResignActive(_ scene: UIScene) {
}
func sceneWillEnterForeground(_ scene: UIScene) {
}
func sceneDidEnterBackground(_ scene: UIScene) {
}
}
复制代码
Application Scene Manifest
,找到Main Storyboard file base name
设置的 Storyboard。ViewController.swift
之间的联系(一个界面与一个类文件关联)。Main Storyboard file base name
和Application Scene Manifest
最里层的Storyboard Name
。willConnectToSession
中纯代码初始化 UIWindow,并设置显示的第一个控制器。window —> rootViewController —> UIViewController —> UIView
。iOS 开发中,一个界面就是一个 UIViewController(视图控制器),界面上显示的内容就是 UIView(视图)。session
UIViewController 中默认有一个和屏幕同样大的 UIView,UIViewController 管理着它的生命周期。全部放在界面上的 UI 控件都放在 UIViewController 的 UIView 之上,在 UIViewController 的代码中能够经过self.view
属性获取它。开发中的其余 UIView(及其子类)都放在该view
上。app
viewDidLoad
:View 完成内存加载。viewWillAppear
:View 即将显示。viewDidAppear
:View 彻底显示。viewWillDisappear
:View 即将消失。viewDidDisappear
:View 完全消失。获取屏幕大小框架
UIScreen.main.bounds
复制代码
所在容器View.viewWithTag
方法拿到这个视图。
- 宽度或者高度其实为0。
注意代码的书写位置,每每有人因为书写的位置不对致使代码报错。函数
如何在代码中获取 Storyboard 中的自定义 UIView?目前有两种方式:工具
有没有更加友好、更加直观、更加便捷、更加高效的方法呢?答案是确定的,那就是 @IBOutlet 与 @IBAction。
Storyboard 中的 UIViewController 与想拖拽的类进行了关联。
- @IBAction:
unrecognized selector sent to instance
- @IBOutlet:
this class is not key value coding-compliant for the key XXX