iOS/Xcode
[Xcode] 스토리보드 없이 코드로 UI 구현하기
startSW
2023. 10. 8. 09:55
1. Main.storyboard 파일을 삭제합니다.
- Move to Trash 버튼을 클릭해서 삭제합니다.
2. info.plist 에서 Stroyboard Name 항목 삭제하기 : '-'(마이너스) 버튼을 클릭하면 됩니다.
3. TARGETS -> Build Settings -> Info.plist Values -> UIKit Main StroyBoard File Base Name -> main 삭제하기
4. SceneDelegate 수정
- 원본
- 수정본
- 원본 코드
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 as? UIWindowScene) else { return }
}
- 수정된 코드
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 }
window = UIWindow(windowScene: windowScene)
window?.rootViewController = ViewController()
window?.makeKeyAndVisible()
}
5. ViewContoller의 Background를 수정합니다.
- 원본
- 수정본
- 수정된 코드
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
self.view.backgroundColor = .systemBlue
}
}
6. 앱을 실행해서 ViewController의 Background가 변경되었는지 확인합니다.
* 중요 *
- LanchScreen 은 절대로 삭제하면 안됩니다.
만약 삭제하면 화면이 작아지는 문제가 발생할 수 있습니다.
원인은 앱이 기준이 되는 윈도우를 LauchScreen으로 사용해서라고 합니다.