Swift 项目初始化
swift
工程和OC
的iOS
工程类似,
-
新建 Swift 项目
File -> New -> Project ,选择iOS-APP
-
Language 选择 Swift ,创建项目
-
修改 AppDelegate.swift 代码,创建导航栏和
rootViewController
。移除AppDelegate
里其余方法,只保留如下方法。
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
self.window = UIWindow(frame: UIScreen.main.bounds);
let navigationController = UINavigationController(rootViewController: ViewController())
self.window?.rootViewController = navigationController
self.window?.backgroundColor = .white
self.window?.makeKeyAndVisible()
return true;
}
}
ViewController.swift
为加载的首页,可以在这个页面加点背景颜色和按钮。
import Foundation
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
self.view.backgroundColor = .red;
let label = UILabel(frame: CGRect(x: 100, y: 200, width: 300, height: 100))
label.text = "This is First VC";
label.textColor = .black;
self.view .addSubview(label);
}
}
- 删除
Info.plist
文件中的Application Scene Manifest
项。 - 然后就可以
Run
了。