问题
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:
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 ?
回答1:
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.
回答2:
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
}
}
回答3:
In the AppDelegate.swift
, enter the following code:
UIPickerView.appearance().semanticContentAttribute = .forceLeftToRight
回答4:
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.
回答5:
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];
}
来源:https://stackoverflow.com/questions/35577317/uidatepicker-flips-on-ios9-rtl