【问题标题】:UIDatePicker Flips on iOS9 RTLiOS9 RTL 上的 UIDatePicker 翻转
【发布时间】:2016-02-23 12:13:43
【问题描述】:

在 RTL 上显示 DatePicker - 时间模式时,我遇到了一个奇怪的问题。

我以编程方式显示日期选择器。 分钟应该在右边,小时应该在左边,在下图中你可以看到它被翻转了:

它发生在 iOS 9 及更高版本上。

我正在使用的代码:

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];

有什么建议吗?

【问题讨论】:

  • 添加您的选择器方法。
  • - (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]; } [自我重载通知]; }

标签: ios ios9 rtf uidatepicker picker


【解决方案1】:

我通过在每个日期选择器子视图上强制 forceRightToLeft 解决了这个问题。

尝试以下方法,它应该可以工作:

override func viewDidLayoutSubviews() {
    for currentView in datePicker.subviews {
        currentView.semanticContentAttribute = .forceRightToLeft
    }
} 

【讨论】:

    【解决方案2】:

    我认为这是您在 UIViewappearance 代理上设置 .ForceRightToLeft 的副作用。

    如果是这种情况,请注意这不是受支持的用例。您的应用应仅在以下情况下处于 RTL:

    a) 本地化为 RTL 语言,并且

    b) 设备的系统语言设置为该语言。

    【讨论】:

    • ForceRightToLeft 对视图没有影响。我尝试删除/更改它,但没有任何改变。
    • 我不确定我是否理解。我的问题是:您是否在任何地方调用此代码? [UIView appearance].semanticContentAttribute = UISemanticContentAttributeForceRightToLeft
    • 实际上我在我的 AppDelegate 上使用[[UIView appearance] setSemanticContentAttribute:UISemanticContentAttributeForceLeftToRight];'。但我必须使用它,因为我有一些必须从左到右的视图。
    • 这不是受支持的配置。如果某些视图必须是从左到右的,那么您必须在每个视图上单独设置它们。
    • wakachamo RTL 在第一次运行应用程序时不适用于 UIDatePicker。
    【解决方案3】:

    AppDelegate.swift中,输入以下代码:

    UIPickerView.appearance().semanticContentAttribute = .forceLeftToRight
    

    【讨论】:

      【解决方案4】:

      这是在这种情况下导致问题的子视图之一。 在所有选择器子视图上强制从左到右将为您解决此问题。

      【讨论】:

        【解决方案5】:

        您可以从 AppDelegate 强制所有 UIDatePicker 类为 RTL。

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

        【讨论】:

          猜你喜欢
          • 2015-12-19
          • 1970-01-01
          • 2021-06-05
          • 2016-07-06
          • 2012-10-20
          • 2014-09-10
          • 1970-01-01
          • 2019-05-13
          • 1970-01-01
          相关资源
          最近更新 更多