티스토리 뷰

반응형

[ 원하는 것 ]

 

앱 전체 강제 라이트모드 && 몇개 일부 화면만 다크모드 지원하고 싶음

(다크모드 지원하는 화면 수가 전체 화면 수에 비해 매우 매우 적기 때문에
entire app을 라이트로 강제하고 몇개 화면들만 예외처리를 해주고 싶음) 

 

 

[ 이슈 ] 

 

1.  AppDelegate 또는 SceneDelegate에서 window의 overrideUserInterfaceStyle 를 light 로 설정

2.  A 화면(뷰컨트롤러)의 viewDidLoad 또는 이니셜라이저에서 overrideUserInterfaceStyle 를 undefined 로 설정

하면 시스템 설정에 따라 A 화면의 라이트/다크가 안바뀜

 

 

debug logging을 켜고 돌려보자! (-UITraitCollectionChangeLoggingEnabled YES)

A 화면에서 시스템 세팅을 바꿔도 로그가 안찍힘 

 

https://developer.apple.com/videos/play/wwdc2019/214/

 

 

 

문서를 보자. 

unspecified를 하면 뷰가 parent view 또는 viewcontroller의 interface style을 상속받는다.
다른 값을 설정하면 새로운 스타일이 해당 뷰와 그것의 모든 서브뷰에 적용된다. 

(UIUserInterfaceStyle.unspecified, which causes the view to inherit the interface style from a parent view or view controller. If you assign a different value, the new style applies to the view and all of the subviews owned by the same view controller)

 

1. unspecified 가 이름그대로 시스템 설정에 따라 정해지는 style이 되기 위해서는 parent가 없을 때 (즉 상속받는 style이 없을 때) 또는 parent가 unspecified 일 때만 가능한 것 같다. 

 

- A 화면을 unspecified 로 지정했음에도 시스템 설정에 따라 style이 안바뀌는 이유는 
window의 light style 을 상속받게 되었으므로!  ( A 화면은 window의 rootViewController) 

 

 

 

2. 위의 문서에서 다른 값을 설정한다는 것은 light, dark (명시적인 style)을 말하는 듯하다. unspecified 말고

 

- window를 light로 해주고 A 화면을 dark 로 지정해보면 A 화면만 다크로 잘 바뀌기 때문 

 

 

[  해결책  ]

 

우선 window의 overrideUserInterfaceStyle를 설정안해줬다.  (default값은 undefined이므로 undefined로 명시적으로 설정해줘도 무방하다.) 

 

 

그리고 이런 프로토콜을 만들고 

protocol DarkModeSupportable where Self: BaseViewController {
    
}

 

 

BaseViewController를 쓰고 있어서 여기 이런 로직을 넣어줌 

class BaseViewController: UIViewController {
    
    ... 
    
    override func viewDidLoad() {
        super.viewDidLoad()

        if self is DarkModeSupportable {
            self.overrideUserInterfaceStyle = .unspecified
        } else {
            self.overrideUserInterfaceStyle = .light
        }
    }
  }

 

 

그리고 몇개의 다크 지원해야하는 화면들만 이 프로토콜을 채택해주도록. 

class AViewController: BaseViewController, DarkModeSupportable {


}

 

 

 

[ 더 테스트해봐야할 것 ]

 

구글링하다가 백그라운드 갔다오면 window에 설정해준 overrideUserInterfaceStyle 이 안먹는 것 같다는 글,,, 

테스트해보기!

 

https://developer.apple.com/forums/thread/672707

 

iPad overrideUserInterfaceStyle no… | Apple Developer Forums

  You’re now watching this thread and will receive emails when there’s activity. Click again to stop watching or visit your profile/homepage to manage your watched threads.   You’ve stopped watching this thread and will no longer receive emails whe

developer.apple.com

 

반응형
댓글