개발아 담하자

[iOS/Swift] storyboard 없이 UI 그리기 (iOS 13 버전 이하) 본문

📱 iOS

[iOS/Swift] storyboard 없이 UI 그리기 (iOS 13 버전 이하)

choidam 2021. 9. 12. 17:00

iOS 13 버전부터 SceneDelegate 이 AppDelegate 역할을 분담하게 되면서

스토리보드 없이 UI를 그릴 때 SceneDelegate 에 처리를 해주어야 하는데요!

 

13버전 이상 부터는 검색하면 자료가 정말 많이 나오는데.. 13버전 이하는 자료가 별로 없어서 정리하려고 합니다 ㅎ_ㅎ

 

1. 프로젝트 > General > Deployment Info 에서 Main Interface 에 적혀있던 Main 을 지워줍니다.

 

Info.plist

2. Info.plist 파일에서

Application Scene Manifest > Scene Configuration > Application Session Role > Item0 > Storyboard Name

행 전체를 삭제합니다.

 

3. AppDelegate.swift 파일 분기처리

import UIKit

@main
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        
        if #available(iOS 13, *) {
            
        } else {
            window = UIWindow()
            let rootVC = ViewController()
            window?.rootViewController = rootVC
            window?.makeKeyAndVisible()
        }
        
        return true
    }

    // MARK: UISceneSession Lifecycle

    @available(iOS 13.0, *)
    func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
        // Called when a new scene session is being created.
        // Use this method to select a configuration to create the new scene with.
        return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
    }

    @available(iOS 13.0, *)
    func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
        // Called when the user discards a scene session.
        // If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.
        // Use this method to release any resources that were specific to the discarded scenes, as they will not return.
    }


}

didFinishLaunchingWithOptions 함수에서 iOS 13버전에 대해 분기처리를 해줍니다.

 

iOS 13버전 이상 부터는 AppDelegate, SceneDelegate 파일 모두 호출이 되므로

iOS 13버전 이상인 경우인 지금은 코드를 작성하지 않습니다.

 

4. SceneDelegate.swift 파일 분기처리

class SceneDelegate: UIResponder, UIWindowSceneDelegate {

    var window: UIWindow?

    @available(iOS 13.0, *)
    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 scene = (scene as? UIWindowScene) else { return }
        
        window = UIWindow(windowScene: scene)
        let rootVC = ViewController()
        window?.rootViewController = rootVC
        window?.makeKeyAndVisible()
    }

    @available(iOS 13.0, *)
    func sceneDidDisconnect(_ scene: UIScene) {
        // Called as the scene is being released by the system.
        // This occurs shortly after the scene enters the background, or when its session is discarded.
        // Release any resources associated with this scene that can be re-created the next time the scene connects.
        // The scene may re-connect later, as its session was not necessarily discarded (see `application:didDiscardSceneSessions` instead).
    }

    @available(iOS 13.0, *)
    func sceneDidBecomeActive(_ scene: UIScene) {
        // Called when the scene has moved from an inactive state to an active state.
        // Use this method to restart any tasks that were paused (or not yet started) when the scene was inactive.
    }

    @available(iOS 13.0, *)
    func sceneWillResignActive(_ scene: UIScene) {
        // Called when the scene will move from an active state to an inactive state.
        // This may occur due to temporary interruptions (ex. an incoming phone call).
    }

    @available(iOS 13.0, *)
    func sceneWillEnterForeground(_ scene: UIScene) {
        // Called as the scene transitions from the background to the foreground.
        // Use this method to undo the changes made on entering the background.
    }

    @available(iOS 13.0, *)
    func sceneDidEnterBackground(_ scene: UIScene) {
        // Called as the scene transitions from the foreground to the background.
        // Use this method to save data, release shared resources, and store enough scene-specific state information
        // to restore the scene back to its current state.
    }


}

sceneDelegate 파일에서 iOS13 버전 이상일 경우를 처리합니다.

 

5. (생략 가능) 이제 Main.storyboard 파일은 사용하지 않으므로 삭제합니다.

 

 

이렇게 하면 기본적인 설정이 끝납니다!

 

혹시 틀린 부분이 있다면 댓글로 남겨주세용

 

'📱 iOS' 카테고리의 다른 글

[iOS] IDFA 적용하기  (0) 2021.10.29
[iOS] Fridump3 를 사용한 메모리 덤프  (0) 2021.10.01
[iOS/Swift] xcworkspace 파일 추가하기  (0) 2021.09.12
[Swift] 의존성 주입 (DI) 이란?  (3) 2021.07.21
[iOS/Swift] Unit Test 란?  (0) 2021.07.19