I am doing a bible reader application for iPhone, I want to implement a “night mode”.
I have done the button for night mode, the reading pane is a tableview, but other pages are also there in my app, my need is when the night mode is switched on the enire background of the view have to be changed to black and text appers to be in white.
I have put a dark gradient image view in the backside of the tableview and set it to hidden, and when the button is tapped I execute darkgradient.hidden = no; so the png will unhide and show behind the tableview as black.
I put the tableview background as clear color. The tableview cell has a label in it called chapterandverse label. I put this code in tableview,to change the background and change the text color in cellForRowAtIndexpath:
if (imagedarkbackground.hidden == NO) {
cell.chapterAndVerse.backgroundColor= [UIColor blackColor];
cell.chapterAndVerse.textColor = [UIColor whiteColor];
table.backgroundColor = [UIColor blackColor];
cell.textLabel.textColor = [UIColor whiteColor];
}
else{
//default
}
The problem is, when I switch to another page and come back to the tableview page the black background changes to default. I know this is not the right way to set a nightmode in app, and also it changes the tableview background not the entire app background view, so how do I do this in a correct way?
If the background color is getting reset to default, your UITableViewController subclass is probably getting unloaded. Are you showing it as a modal view? If so you’ll have to save the night mode setting somewhere else, like a BOOL property on your app delegate. Then in your viewDidLoad, check that property and set the background color accordingly. Better still, save the value so if your app quits, when it comes back, night mode is still set to the same thing, but maybe not if a lot of time has passed since the app was last used.
Better still, automatically set night mode based on the clock time. Yet better again, use data from the camera to detect the ambient light level. But in either case a button to override is also nice.