首頁 > 軟體

iOS開發刪除storyboard步驟詳解

2022-11-07 14:00:14

刪除iOS專案中的storyboard

刪除專案中的storyboard, (變成一個純程式碼的iOS UIKit專案), 需要幾步?

  • 找到storyboard, 刪掉它.
  • 直接用ViewController.

刪除storyboard

  • 首先, 你得有(新建)一個storyboard專案.
  • 刪除storyboard. 選"Move to Trash".
  • 刪除plist中的storyboard name.

  • 刪除deploy target中的Main Interface, 本來是”main”, 把它變為空.

(截圖換了一個專案名, 不要在意這些細節.)

用上自己的ViewController

在ViewController裡寫上自己的完美View. 比如:

import UIKit
class ViewController: UIViewController {
    override func loadView() {
        view = UIView()
        view.backgroundColor = .systemBlue
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }
}

設定新的rootViewController.

  • SceneDelegate中設定rootViewController. (iOS 13)
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
    var window: UIWindow?
    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
        // Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
        // If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
        // This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
        guard let windowScene = (scene as? UIWindowScene) else { return }
        let window = UIWindow(windowScene: windowScene)
        window.rootViewController = ViewController()
        self.window = window
        window.makeKeyAndVisible()
    }
 ...
  • tvOS沒有SceneDelegate (或者你想要相容iOS 13以前的舊版本):
import UIKit
@main
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
    func application(_: UIApplication, didFinishLaunchingWithOptions _: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        window = UIWindow(frame: UIScreen.main.bounds)
        window?.rootViewController = ViewController()
        window?.makeKeyAndVisible()
        return true
    }
...

執行程式, 看到自己在ViewController裡設定的View.

以上就是iOS開發刪除storyboard步驟詳解的詳細內容,更多關於iOS刪除storyboard步驟的資料請關注it145.com其它相關文章!


IT145.com E-mail:sddin#qq.com