iT邦幫忙

2021 iThome 鐵人賽

DAY 16
0
Mobile Development

ios 的小小實驗室 !系列 第 16

DAY 16 『 改用 xib 進行界面創作 』

  • 分享至 

  • twitterImage
  •  
  • storyboard 有三個概念是最容易混淆:

    1. xib:實際是個xml文件,xib = XML nib,可通過拖拽文件進行界面創作和佈局。
    2. nib:xib編譯之後就得到nib文件,nib= NeXT Interface Builder
    3. storyboard:類似升級版的xib,可以同時管理多個xib文件。
  • storyboard VS xib:

    storyboard 注重於多個頁面的層級關係及跳轉
    xib 注重於單個頁面的佈局,若多人開發同個專案,xib 比較推薦


先建專案

設定存檔路徑

New Group

New File

選Cocoa Touch Class

p.s: MainVC = Main ViewController

info.plist -> Application Scene Manifest -> Scene Configuration -> Application Session Role -> Item0 ( Default Configuration ) -> 刪掉 Storyboard Name

選擇xib檔

找到 SceneDelegate.swift 下的 scene 函數

修改為此

    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
        
        guard let windowScene = (scene as? UIWindowScene) else { return }
                
                // MARK: Without NavigationController
                // let rootVC = MainVC(nibName: String(describing: MainVC.self), bundle: nil)
                // MARK: With NavigationController
                 let rootVC = UINavigationController(rootViewController: MainVC())
                
                window = UIWindow(frame: windowScene.coordinateSpace.bounds)
                window?.windowScene = windowScene
                window?.rootViewController = rootVC //navController
                window?.makeKeyAndVisible()
        
        // 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).

    }

刪除這兩個檔案

測試:
在MainVC.xib檔下新增一個Lable,拉Outlet到Main.swift檔下

燒到模擬器


這樣就完成 Xcode 的 xib 環境建置了!明天會分享 push 、 present 轉畫面,敬請期待!


上一篇
DAY 15 『 Realm 新增、修改、刪除 』Part3
下一篇
DAY 17 『 Xib 畫面跳轉 - push 、 present 』
系列文
ios 的小小實驗室 !30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言