반응형
iOS에서 다크 모드를 확인하는 방법은?
- iOS 앱에서 다크 모드 상태를 관찰하는 방법
- iOS 앱에서 다크 모드 상태 변화에 대응하는 방법
UIKit은 UITrait Collection을 한동안 보유하고 있습니다.iOS 9부터 UITrait Collection을 사용하여 기기가 3D Touch를 지원하는지 여부를 확인할 수 있습니다(다른 날의 슬픈 대화).
iOS 12에서 UITraitCollection은 다음과 같은 새로운 속성을 얻었습니다.var userInterfaceStyle: UIUserInterfaceStyle
다음 세 가지 경우를 지원합니다.light
,dark
,그리고.unspecified
UIViewController가 UITraitEnvironment를 상속하므로 ViewController의traitCollection
. 본매장userInterfaceStyle
.
또한 UITrait Environment에는 상태 변경이 발생했을 때 코드를 해석하는 데 도움이 되는 몇 가지 훌륭한 프로토콜 스텁이 있습니다(사용자가 어두운 쪽에서 밝은 쪽으로 전환하거나 그 반대로 전환할 때).다음은 당신을 위한 멋진 코딩 예입니다.
class MyViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
if self.traitCollection.userInterfaceStyle == .dark {
// User Interface is Dark
} else {
// User Interface is Light
}
}
override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
// Trait collection has already changed
}
override func willTransition(to newCollection: UITraitCollection, with coordinator: UIViewControllerTransitionCoordinator) {
// Trait collection will change. Use this one so you know what the state is changing to.
}
}
프로젝트에서 다음 코드를 사용하여 빛 또는 어두운 모드를 확인할 수 있습니다.
func viewDidLoad() {
super.viewDidLoad()
switch traitCollection.userInterfaceStyle {
case .light, .unspecified:
// light mode detected
case .dark:
// dark mode detected
}
}
인터페이스 스타일의 변경사항을 확인할 수도 있습니다.
override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
super.traitCollectionDidChange(previousTraitCollection)
let userInterfaceStyle = traitCollection.userInterfaceStyle // Either .unspecified, .light, or .dark
// Update your user interface based on the appearance
}
Mojave 이후의 macOS에서와 마찬가지로 자산 카탈로그에서 명암 모드에 대한 이미지를 정의하여 해당 이미지를 자동으로 사용할 수 있습니다.
여기서 찍었습니다.
언급URL : https://stackoverflow.com/questions/56457395/how-to-check-for-dark-mode-in-ios
반응형
'programing' 카테고리의 다른 글
호출 함수에서 jquery ajax 콜백을 기다립니다. (0) | 2023.09.24 |
---|---|
Mariadbin Centos 7을 사용한 INNER JOIN 사용자, 데이터베이스 및 데이터베이스 크기 (0) | 2023.09.24 |
XL Connect with R Shiny에서 Excel 파일 다운로드 (0) | 2023.09.24 |
Spring MVC에서 요청 파라미터에 대한 날짜 전달 (0) | 2023.09.24 |
스크롤을 비활성화한 웹사이트에서 스크롤을 보편적으로 활성화하는 방법은? (0) | 2023.09.24 |