4

I have a weird issue when displaying a DatePicker - Time mode on RTL.

Im displaying the date picker programmatically. Minutes should be on the right side, and hours on the left, and on the following image you can see it's flipped:

enter image description here

It happens on iOS 9 and above.

The code I'm using:

UIDatePicker *datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height+44, self.view.frame.size.width, 216)];
datePicker.tag = tag;
[datePicker setSemanticContentAttribute:UISemanticContentAttributeForceLeftToRight];
[datePicker setDatePickerMode:UIDatePickerModeTime];
[datePicker setBackgroundColor:[UIColor whiteColor]];
[datePicker addTarget:self action:@selector(changeDate:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:datePicker];

Any suggestions ?

user3660992
  • 113
  • 1
  • 7
  • add your selector method. – jerfin Feb 23 '16 at 12:19
  • - (void)changeDate:(UIDatePicker *)sender { NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"HH:mm"]; [dateFormatter setLocale:[NSLocale systemLocale]]; [dateFormatter setTimeZone:[NSTimeZone systemTimeZone]]; NSString *capturedStartDate = [dateFormatter stringFromDate:sender.date]; if (sender.tag == startDayDP) { [_startDayHour setText:capturedStartDate]; } else { [_endDayHour setText:capturedStartDate]; } [self ReloadNotifications]; } – user3660992 Feb 23 '16 at 12:21

5 Answers5

6

I solved it by forcing forceRightToLeft on each date picker sub views.

Try the following and it should work:

override func viewDidLayoutSubviews() {
    for currentView in datePicker.subviews {
        currentView.semanticContentAttribute = .forceRightToLeft
    }
} 
Hexfire
  • 5,945
  • 8
  • 32
  • 42
Itay Gardi
  • 61
  • 2
  • 2
4

I think this is a side-effect of your setting .ForceRightToLeft on UIView's appearance proxy.

If that's the case, please note that this is not a supported use case. Your app should only be in RTL if it's:

a) localized into an RTL language, and

b) the device's system language is set to that language.

wakachamo
  • 1,723
  • 1
  • 12
  • 18
  • 1
    The `ForceRightToLeft ` has no effect on the view. I have tried to remove it / change it, and nothing changed. – user3660992 Feb 25 '16 at 07:58
  • I'm not sure I understand. My question is: Are you calling this code anywhere? ```[UIView appearance].semanticContentAttribute = UISemanticContentAttributeForceRightToLeft``` – wakachamo Feb 25 '16 at 18:24
  • Actually I'm using `[[UIView appearance] setSemanticContentAttribute:UISemanticContentAttributeForceLeftToRight];'` on my AppDelegate. But i have to use it, because i have some views that must be left to right. – user3660992 Feb 26 '16 at 09:53
  • This is not a supported configuration. If some views must be left-to-right, then you must set them individually on each view. – wakachamo Feb 26 '16 at 18:49
  • wakachamo RTL does not work for UIDatePicker when first time run the app. – user3182143 Mar 22 '17 at 13:55
  • user3182143 did you ever find a solution for UIDatePicker being flipped? I mean the minutes are before hour in controller after forcing appdelegate reload root and RTL – ares777 Sep 15 '17 at 06:57
  • Ok, figured out, for view in yourdatepicker.subviews { let USERDEF = NSUserDefaults.standardUserDefaults() if USERDEF.integerForKey("CHOOSEN_LANGUAGE") == 0 { view.semanticContentAttribute = .ForceRightToLeft } else { //this must fix uipicker being flipped view.semanticContentAttribute = .ForceLeftToRight } } – ares777 Sep 19 '17 at 11:55
2

In the AppDelegate.swift, enter the following code:

UIPickerView.appearance().semanticContentAttribute = .forceLeftToRight
Merichle
  • 693
  • 11
  • 15
0

it's one of the subviews that caused the problem in this case. forcing left to right on all of the pickers subview will fix that for you.

Nevgauker
  • 241
  • 1
  • 12
0

You can force all UIDatePicker classes to be RTL from the AppDelegate.

if(SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"9.0")) {
    [[UIView appearanceWhenContainedIn:[UIDatePicker class], nil] setSemanticContentAttribute:UISemanticContentAttributeForceRightToLeft];
}
Elad
  • 594
  • 3
  • 13