본문 바로가기
공부/Apple

[UIKit] 다크모드, 라이트 모드 고정

by 초코팅촉 2024. 9. 1.
728x90

이번에는 다크모드나 라이트 모드에 고정하는 방법을 알아보고자 합니다.

 

우리가 앱을 만들다보면 앱의 테마를 위해

다크모드나 라이트 모드로 고정해야 하는 상황이 생길수도 있습니다. 그쵸? 저만 그런거아니죠?

 

예시코드

UIKit에서는 간단한 방법으로 고정 할 수 있는데요, 바로 SceneDelegate를 이용하면 됩니다.

아래는 예시 코드입니다.

window.overrideUserInterfaceStyle = .light // 다크모드는 .dark

어때요, 간단하죠?

 

전체 예시

이 한줄의 코드를 `scene(_:willConnectTo:options:)`안에 적어주면 됩니다.

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
	window?.overrideUserInterfaceStyle = .dark // 이렇게 하면 다크모드
	guard let _ = (scene as? UIWindowScene) else { return }
}

 

아래의 글을 참고해 작성되었습니다.

https://jiwift.tistory.com/entry/iOSSwift-화면-다크모드-밝은모드-고정

 

[iOS/Swift] 다크모드, 라이트모드 고정 overrideUserInterfaceStyle

[iOS/Swift] 화면 다크 모드, 밝은 모드 고정 if #available(iOS 13.0, *) { window?.overrideUserInterfaceStyle = .light } 위 코드 ‘.light’ 부분을 dark로 바꾸면 다크 모드로 고정된다. 현재 SceneDelegate willConnectTo에 작

jiwift.tistory.com

 

728x90