[iOS] - Storyboard와 SwiftUI 없이 Project 만들기

iOS 2020. 7. 31. 21:27

● 주제 : Storyboard를 사용하지 않고 소스만으로 프로젝트를 만들어 보려 합니다.

  • 왜 Storyboard 없이 소스만으로 프로젝트를 만들려고 할까?
  1. git과 같은 형상관리 툴을 사용해서 협업을 할 때 심심 찬 게 Storyboard의 영역에서 충돌(conflict)이 발생하고, 이를 해결하기 위한 시간적 비용도 많이 소비됩니다.
  2. 복잡한 Layout을 표현할 때 소스로 구현하면 제약조건(Constraint)을 쉽게 적용하고 추적할 수가 있습니다. 소스로 구현하다 보면, 자연스럽게 ViewController도 재사용을 하게 되고요.
  • 할 일
  1. 프로젝트 만들기
  2. sceneDelegate 삭제 (프로젝트에 등록된 정보와 소스파일 삭제)
  3. 새 ViewController 만들기 ("MyRootView.swift" , "MyRootView.xib" 생성과 두 파일과의 연결) 
  4. AppDelegate 수정 (앱의 기본 화면이 될 "MyRootView"와 UINavigationController를 등록)

 

  • 프로젝트 만들기 - 아래 그림과 같이 User Interface의 값은 "Storyboard"로 설정합니다.

  • sceneDelegate 등록 정보 삭제 - "Info.plist"의 Application Scene Manifest" 항목 삭제 -> "-" 버튼 눌러서 프로젝트에 등록된 기본 화면 정보를 삭제합니다.

  •  sceneDelegate 소스 파일 삭제 - 위 내용에 이어서 실제 "sceneDelegate.swift"파일을 삭제합니다.

  • 새 ViewController 만들기 : "MyRootView.swift" -  새 파일 만들기 메뉴 경로 "File -> New -> File..."로 이동하여"  "Swift File"을 선택하고, "Next"버튼을 누른 후 "MyRootView.swift 2)"처럼 Save as에 MyRootView와 같이 파일명을 입력 후 "Create" 버튼을 누릅니다.

 

"MyRootView.swift" 1)
"MyRootView.swift" 2)

  • 새 ViewController - "MyRootView.swift" 파일 작성
//UIKit 추가
import UIKit

import Foundation

//Class 생성과 UIViewController 상속
class MyRootView: UIViewController {
    
    //viewDidLoad 오버라이드 
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // 실행시 초록색 화면이 보입니다. (메인화면을 잘 봐꿨는지 확인용.)
        self.view.backgroundColor = UIColor(red: 0, green: 1, blue: 0, alpha: 0.3)
    }
}
  • 새 ViewController 만들기 : "MyRootView.xib" - 새 파일 만들기 메뉴 경로 "File -> New -> File..."로 이동하여"  "View" 선택하고, "Next"버튼을 누른  "MyRootView.xib 2)"처럼 Save as에 MyRootView와 같이 파일명을 입력 후 "Create" 버튼을 누릅니다.

 

 "MyRootView.xib" 1)
"MyRootView.xib" 2)

  • 새 ViewController 만들기 : "MyRootView.xib의 File's Owner의 Custom Class에 MyRootView.swift 연결"

MyRootView.xib 파일 선택 -> File's Owner 선택 -> Custom class 에 "MyRootView" 입력
그림의 "View" icon을 우클릭 -> Referencing Outlets "+"를 File's Owner로 Drag 합니다.

  • AppDelegate 수정 - "UISceneSession Lifecycle"의 관련된 내용을 삭제합니다. 

      - 참고로, SwiftUI는 이전 버전과의 호환성이 없는 문제점이 있습니다. (단지 제 의견이다만,) 시간이 지나고 SwiftUI 가 조금 더 다듬어지고 유연해진다면 SwiftUI는 대중화될 것입니다. 하지만 하위 버전의 호환성을 고려하지 않고 개발하는 경우가 아니면 호환성을 고려해서 SwiftUI를 덜어 내는 것이 정신 건강상 좀 더 좋을 것 같습니다. ^^;

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    func application(_ application: UIApplication, 
                     didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        return true
    }

    /** 여기부터 지움... (UISceneSession 관련 내용)
    // MARK: UISceneSession Lifecycle

    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)
    }

    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.
    }
    여기까지... (UISceneSession 관련 내용)
    */

}
  • AppDelegate 수정 - MyRootView와 UINavigationController 등록
import UIKit
import Foundation

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    // rootViewController에 navigation 을 등록할 UIWindow. 
    var window: UIWindow?
    // 화면 이동과 화면 이력을 관리할 UINavigationController 추가.
    var navigation : UINavigationController? = nil
    // 앱이 시작 할 때 보여 줄 기본 화면 (이름을 MyRootView 로 정함.)
    var myRootView : MyRootView!

    func application(_ application: UIApplication, 
                     didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        
        // 아래 내용 작성.
        myRootView = MyRootView()
        navigation = UINavigationController.init(rootViewController: myRootView)
                          
        // 상단 네비게이션바 숨김.                  
        navigation?.setNavigationBarHidden(true,animated: false) 
        window = UIWindow(frame: UIScreen.main.bounds)
        window?.rootViewController = navigation
      
        return true
    }

    /** 여기부터 지움... (UISceneSession 관련 내용)
    // MARK: UISceneSession Lifecycle

 

  • 결과 화면 - 위 MyRootView의 배경색 설정이 적용된 ViewController로 적용이 되어서 실행되는 것을 확인할 수 있습니다.

 

*^^* 여기까지 읽어주신 독자님께 감사드립니다. iOS 개발을 배워 나가며 쓰는 글입니다. 글의 내용 중 틀린 내용이 있거나 부족한 점 있다면 귀뜸 부탁드립니다. 

다음 글은 Snapkit의 project 등록과 사용법 공부해 보겠습니다.

'iOS' 카테고리의 다른 글

[iOS] - UITextView  (0) 2020.08.02
[iOS] - UILabel  (0) 2020.08.02
[iOS] - SnapKit : Layout 정리  (0) 2020.08.01
[iOS] - CocoaPods(코코아팟), Xcode의 Library 관리  (0) 2020.08.01

설정

트랙백

댓글