UIViewController with custom statusBar
- statusBar의 textColor와 backgroundColor 변경
import UIKit
class ViewController: UIViewController {
// MARK: - Life Cycle
override func viewDidLoad() {
super.viewDidLoad()
/// custom statusBar
setupCustomStatusBar()
}
// MARK: - Style
override var preferredStatusBarStyle: UIStatusBarStyle {
/// statusBar textColor 흰색으로 설정
return .lightContent
/// statusBar textColor 검정색으로 설정
//return .darkContent
}
// MARK: - Setup
private func setupCustomStatusBar() {
if #available(iOS 13.0, *) {
/// statusBar height 설정
/// 'statusBarFrame' was deprecated in iOS 13.0: Use the statusBarManager property of the window scene instead.
let statusBarManager = view.window?.windowScene?.statusBarManager
let statusBarHeight = statusBarManager?.statusBarFrame.size.height ?? 0
/// custom statusBar view 설정
let statusBarView = UIView()
statusBarView.backgroundColor = UIColor.lightGray /// 원하는 배경색 설정
view.addSubview(statusBarView)
/// autoLayout
statusBarView.translatesAutoresizingMaskIntoConstraints = false
statusBarView.heightAnchor.constraint(equalToConstant: statusBarHeight).isActive = true
statusBarView.widthAnchor.constraint(equalTo: view.widthAnchor, multiplier: 1.0).isActive = true
statusBarView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
statusBarView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
} else {
let statusBar = UIApplication.shared.value(forKeyPath: "statusBarWindow.statusBar") as? UIView
statusBar?.backgroundColor = UIColor.lightGray /// 원하는 배경색 설정
}
}
}