ios - Using an enum to execute a function? -
i want call function in viewdidload() in view controller set behavior of view controller's status bar. want status bar have 3 behavioral states: regular, stay light, stay dark.
// view controller view did load override func viewdidload() { super.viewdidload() // status bar behavior // call function setneedsstatusbarappearanceupdate() }
i don't have experience enums please tell me how accomplish i'm trying accomplish. ultimately, want call 1 function in viewdidload() depending on behavior of status bar want view controller (regular, stay light, stay dark).
enum statusbarbehavior { case regular case staylight case staydark func statusbar() { switch self { // changes status bar depending on variable case .regular: if nighttime == true { uiapplication.shared.statusbarstyle = .lightcontent } else { uiapplication.shared.statusbarstyle = .default } // permanently keeps status bar light case .staylight: uiapplication.shared.statusbarstyle = .lightcontent // permanently keeps status bar dark case .staydark: uiapplication.shared.statusbarstyle = .default } } }
all appreciated.
change enum function statusbar()
return status bar style:
enum statusbarbehavior { case regular case staylight case staydark func statusbar() -> uistatusbarstyle { switch self { // changes status bar depending on variable case .regular: if nighttime == true { return .lightcontent } else { return .default } // permanently keeps status bar light case .staylight: return .lightcontent // permanently keeps status bar dark case .staydark: return .default } } }
create property store enum value desire:
var statusbarbehavior = statusbarbehavior.regular
override following property in view controller:
override var preferredstatusbarstyle: uistatusbarstyle { return statusbarbehavior.statusbar() }
whenever status bar behavior should change, set statusbarbehavior
value .regular
, .staylight
, or .staydark
, call setneedsstatusbarappearance()
update.
also, if have not done so, you'll need set view controller-based status bar appearance
key in info.plist
file yes
.
Comments
Post a Comment