728x90
StoryBoard를 사용하지 않는 경우
storyboard를 사용하지 않고 인터페이스를 생성하고 싶을 땐 AppDelegate 혹은 SceneDelegate에서 window를 직접 생성해주어야 한다.
우선 Main.storyboard를 삭제해준다.
1. AppDelegate에서 window 생성
iOS 13 이상의 OS에서 AppDelegate에서 window 생성 시엔
application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration
application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>)
이 2가지 함수를 삭제 해준다.
그리고 info.plist 파일에서 SceneConfiguration 항목을 지워준다.
그리고 didFinishLaunchingWithOptions 메서드에서 window 객체 생성 후 rootViewController 지정, 그리고 makeKeyAndVisible() 함수를 실행해준다.
//
// AppDelegate.swift
// WebSocket
//
// Created by 박준현 on 2022/01/22.
//
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.
window = UIWindow(frame: UIScreen.main.bounds)
let navVC = UINavigationController(rootViewController: ViewController())
navVC.isNavigationBarHidden = true
window?.rootViewController = navVC
window?.makeKeyAndVisible()
return true
}
// MARK: UISceneSession Lifecycle
/* 아래에 있는 sceneDelegate 코드를 삭제해야한다.
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.
}
*/
}
2. SceneDelegate에서 window생성
willConnectTo 메서드에서 window 객체에 windowScene을 지정 후에 객체를 생성하고 AppDelegate에서 생성 방식과 동일하게 생성해준다.
//
// SceneDelegate.swift
// Practice
//
// Created by 박준현 on 2021/11/28.
//
import UIKit
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).
if let window = scene as? UIWindowScene {
self.window = UIWindow(windowScene: window)
let viewController = ViewController()
let navVC = UINavigationController(rootViewController: viewController )
navVC.isNavigationBarHidden = true
self.window?.rootViewController = navVC
self.window?.makeKeyAndVisible()
}
guard let _ = (scene as? UIWindowScene) else { return }
}
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).
}
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.
}
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).
}
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.
}
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.
}
}
728x90
'iOS > 설명' 카테고리의 다른 글
[iOS] M1 환경에서 pod install 에러 발생 시 대처 (0) | 2022.01.22 |
---|---|
[iOS] 코코아팟 (Cocoapods) 설치 및 사용 (0) | 2022.01.22 |
[iOS] JSON String을 Dictionary로 변환 (0) | 2022.01.08 |
[iOS] UIResponder (0) | 2022.01.04 |
[iOS] Provisioning Profile, Certificate, Code signing (0) | 2021.12.19 |
댓글