steveluoxin
steveluoxin

Reputation: 436

iOS How to reset UITableView's color after changed

I'm writing an iOS app that supports Auto-Night theme, and there is UITableView I need to work on night theme.

When the app enables night theme, I change the UITableView's background color, section header background color, and my customized cell's color.

My question is that when the app disables night theme, how to reset the UITableView's background color and the corresponding section header background color?

I tried to call reloadData method when disabling night theme, but the colors remain night theme.

I have to specifically set these colors to default to make it work, but somehow I feel is not the 'right' way to do it.

Is there any way to reset the UITableView to default colors? Thanks in advance!

Upvotes: 0

Views: 122

Answers (1)

NSAdi
NSAdi

Reputation: 1253

Firstly, there is nothing wrong with setting the color back to original when you disable night mode.

However if you do want to be elegant about it you can set the color of the TableView by using a closure variable which will determine if Dark Mode is on or off and accordingly return a color.

var colorForTableView: (Bool) -> (UIColor) = { isDarkMode in

    let defaultColor = UIColor.white
    let darkColor = UIColor.black

    return isDarkMode ? darkColor : defaultColor
}

Usage:

tableView.backgroundColor = colorForTableView(isDarkMode)

Upvotes: 2

Related Questions