创建 UIKit 项目教程
一、打开 XCode,选择 iOS 下的 App,然后点 Next
二、Interface 选择 Storyboard,然后点 Next
三、删掉 Main.storyboard
四、删掉 SceneDelegate.swift
五、AppDelegate.swift 只保留第一个函数
六、在 AppDelegate.swift 文件里的 application 函数改成这样
class AppDelegate: UIResponder, UIApplicationDelegate {var window: UIWindow?func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {window = UIWindow(frame: UIScreen.main.bounds)window?.rootViewController = ViewController()window?.makeKeyAndVisible()return true}
}
七、删掉在 Info.plist 的 Application Scene Manifest
八、在 building settings 里搜 storyboard,然后把 UIKit Main Storyboard File Base Name 的 "Main" 置空
九、验证
把这段代码复制进 ViewController.swift 中
import UIKitclass ViewController: UIViewController {var label = UILabel()override func viewDidLoad() {super.viewDidLoad()view.backgroundColor = .whiteview.addSubview(label)configLabel()layoutLabel()}func configLabel() {label.text = "Hello UIKit"label.textColor = .blacklabel.font = .systemFont(ofSize: 18)label.textAlignment = .center}func layoutLabel() {label.translatesAutoresizingMaskIntoConstraints = falseNSLayoutConstraint.activate([label.centerXAnchor.constraint(equalTo: view.centerXAnchor),label.centerYAnchor.constraint(equalTo: view.centerYAnchor),label.leadingAnchor.constraint(greaterThanOrEqualTo: view.leadingAnchor, constant: 20),label.trailingAnchor.constraint(lessThanOrEqualTo: view.trailingAnchor, constant: -20)])}
}
会看到如下结果: