비전공자 개발일기

Dark Mode & Light Mode, Current App version check 본문

SWIFT

Dark Mode & Light Mode, Current App version check

HiroDaegu 2023. 1. 9. 15:04
728x90
SMALL

 

[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 }
        // 설정에서 선택된 mode가 활성화 되도록 작성
        let rawValue = UserDefaults.standard.integer(forKey: "Appearance")
        window?.overrideUserInterfaceStyle = UIUserInterfaceStyle(rawValue: rawValue) ?? .unspecified
    }

import UIKit

class SettingViewController: UIViewController {
    
    @IBOutlet weak var modeSelector: UISegmentedControl!
    @IBOutlet weak var currentVersion: UILabel!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        makeUI()
    }
    
    func makeUI() {
        // 현재 어플의 버전 확인
        let versionChk = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as! String
        currentVersion.text = versionChk
        // 현재 설정되어 있는 mode로 보여주는 코드
        let rawValue = UserDefaults.standard.integer(forKey: "Appearance")
        // 해당 rawValue를 Select
        modeSelector.selectedSegmentIndex = rawValue
    }
    
    @IBAction func modeSelect(_ sender: Any) {
    	// Select한 것으로 mode 변경
        view.window?.overrideUserInterfaceStyle = UIUserInterfaceStyle(rawValue: modeSelector.selectedSegmentIndex) ?? .unspecified
        
        // 어플을 재실행해도 마지막으로 설정된 값이 보이도록 셋팅
        UserDefaults.standard.set(modeSelector.selectedSegmentIndex, forKey: "Appearance")
    }
        
}
728x90
LIST

'SWIFT' 카테고리의 다른 글

MVC Pattern  (0) 2023.01.13
UIView Border by storyboard  (0) 2023.01.10
앱 등록 절차  (0) 2023.01.08
App Icon  (0) 2023.01.02
LaunchScreen(Basic + Custom) & sleep  (0) 2023.01.02