From 9351e7149b194e3a1ffab7222a37d08a05e52996 Mon Sep 17 00:00:00 2001 From: Anjana Rajeev Date: Thu, 25 Jun 2020 14:19:38 +0530 Subject: [PATCH] Update date picker background dynamically for Dark Theme in iOS --- src/ios/DatePicker.m | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/src/ios/DatePicker.m b/src/ios/DatePicker.m index caae145..775fe0a 100644 --- a/src/ios/DatePicker.m +++ b/src/ios/DatePicker.m @@ -44,10 +44,13 @@ - (void)show:(CDVInvokedUrlCommand*)command { } - (BOOL)showForPhone:(NSMutableDictionary *)options { + BOOL addListenerForTraitChange; if(!self.datePickerContainer){ [[NSBundle mainBundle] loadNibNamed:@"DatePicker" owner:self options:nil]; + addListenerForTraitChange = YES; } else { self.datePickerContainer.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.4]; + addListenerForTraitChange = NO; } [self updateDatePicker:options]; @@ -91,14 +94,34 @@ - (BOOL)showForPhone:(NSMutableDictionary *)options { animations:^{ self.datePickerComponentsContainer.frame = frame; self.datePickerContainer.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.4]; + [self adjustDatePickerContainerBg]; } completion:^(BOOL finished) { - + + if (addListenerForTraitChange) { + // Without the check, it would add the observer for every single show command + [[NSNotificationCenter defaultCenter] addObserver:self + selector:@selector(applicationEnteredForeground:) + name:UIApplicationDidBecomeActiveNotification + object:nil]; + } }]; return true; } +- (void)applicationEnteredForeground:(NSNotification *)notification { + [self adjustDatePickerContainerBg]; +} + +-(void) adjustDatePickerContainerBg { + if (self.viewController.traitCollection.userInterfaceStyle == UIUserInterfaceStyleDark) { + self.datePickerComponentsContainer.subviews[0].backgroundColor = [[UIColor darkGrayColor] colorWithAlphaComponent:1]; + } else { + self.datePickerComponentsContainer.subviews[0].backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:1]; + } +} + - (BOOL)showForPad:(NSMutableDictionary *)options { self.datePickerPopover = [self createPopover:options]; return true;