xcode iOS9:UIDatePicker 和 UIDatePickerModeTime 的 datePickerMode 只显示小时,没有分钟
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31709525/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
iOS9: UIDatePicker with datePickerMode of UIDatePickerModeTime only shows Hours, and no Minutes
提问by FranticRock
I'm using Xcode beta 7 with the iOS9 simulator. Using a UIDatePicker with a datePickerMode of UIDatePickerModeTime only shows Hours, and not minutes.
我在 iOS9 模拟器上使用 Xcode beta 7。使用带有 UIDatePickerModeTime 的 datePickerMode 的 UIDatePicker 只显示小时,而不是分钟。
See screenshot:
看截图:
On iOS 7 and 8, obviously it works as expected, and shows both Hours and Minutes. Screenshot:
在 iOS 7 和 8 上,显然它按预期工作,并显示小时和分钟。截屏:
I really do not want to reinvent the wheel and roll my own time picker. Any ideas on why this might be happening and how to fix? I can't find anything on google.
我真的不想重新发明轮子并滚动我自己的时间选择器。关于为什么会发生这种情况以及如何解决的任何想法?我在谷歌上找不到任何东西。
thanks, Alex
谢谢,亚历克斯
采纳答案by Andrew Little
Found a useful description of this problem in the iOS 9 release notes - seems I should be reading these more carefully.
在 iOS 9 发行说明中找到了对此问题的有用描述 - 似乎我应该更仔细地阅读这些内容。
UIPickerView and UIDatePicker are now resizable and adaptive—previously, these views would enforce a default size even if you attempted to resize them. These views also now default to a width of 320 points on all devices, instead of to the device width on iPhone. Interfaces that rely on the old enforcement of the default size will likely look wrong when compiled for iOS 9. Any problems encountered can be resolved by fully constraining or sizing picker views to the desired size instead of relying on implicit behavior.
UIPickerView 和 UIDatePicker 现在可以调整大小和自适应——以前,即使您尝试调整它们的大小,这些视图也会强制执行默认大小。这些视图现在在所有设备上也默认为 320 磅的宽度,而不是 iPhone 上的设备宽度。在为 iOS 9 编译时,依赖于旧的默认大小强制实施的接口可能看起来是错误的。遇到的任何问题都可以通过将选择器视图完全限制或调整为所需大小来解决,而不是依赖于隐式行为。
In my case all I had to do was remove all constraints on the UIDatePicker and then "Reset to Suggested Constraints". Rebuild and now all is well.
就我而言,我所要做的就是删除 UIDatePicker 上的所有约束,然后“重置为建议的约束”。重建,现在一切都很好。
回答by LordParsley
I encountered this after the public release of iOS 9.0 with a UIDatePicker using UIDatePickerModeDate in my tableview.
在我的 tableview 中使用 UIDatePickerModeDate 的 UIDatePicker 公开发布 iOS 9.0 后,我遇到了这个问题。
I hacked around it by changing the UIDatePicker mode right before it was displayed, and then changing it back to the desired one:
我通过在显示之前更改 UIDatePicker 模式来绕过它,然后将其更改回所需的模式:
[myDatePicker setDatePickerMode:UIDatePickerModeDateAndTime];
[myDatePicker setDatePickerMode:UIDatePickerModeDate];
I'm guessing redrawing it solves the issue. For interest's sake I don't think it's actually an issue of not displaying the minutes but rather a bug in the subviews because this is what mine looked like:
我猜重绘它解决了这个问题。出于兴趣,我认为这实际上不是不显示会议记录的问题,而是子视图中的错误,因为这是我的样子:
Inspecting using FLEX, this is part of the UIDatePicker that has a solid white background.
使用 FLEX 检查,这是具有纯白色背景的 UIDatePicker 的一部分。
回答by Martyn Davis
I had an issue when upgrading Xcode to 7.0.
将 Xcode 升级到 7.0 时遇到问题。
When the UIDatePicker was displayed the middle portion was blank, as per LordParsley's answer.
根据 LordParsley 的回答,当显示 UIDatePicker 时,中间部分是空白的。
The height of the UIDatePicker for iOS 9 is 216; whereas earlier versions the height is 162, and forcing the height to 162 resolved the issue.
iOS 9 的 UIDatePicker 高度为 216;而早期版本的高度是 162,强制高度为 162 解决了这个问题。
Since my view is defined within a storyboard, I setup a height constraint on the UIDatePicker and set the height to 162 for iOS versions < 9.0, within the view's viewDidLoad.
由于我的视图是在情节提要中定义的,因此我在 UIDatePicker 上设置了高度约束,并将视图的 viewDidLoad 中的 iOS 版本 < 9.0 的高度设置为 162。
- (void) viewDidLoad {
[super viewDidLoad];
if ([[[UIDevice currentDevice] systemVersion] floatValue] < 9.0) {
//
// NOTE: iOS 9, or Xcode 7, now sets UIDatePicker height at 216; whereas,
// iOS 8, or Xcode 6.x, set it at 162.
//
// If the height is left at 216, then on iOS 7.1, the middle portion of the
// UIDatePicker control is missing. Setting the hieght to 162 fixes the issue
// within this application.
//
// UIDatePicker frame cannot be used to adjust the size, therefore use a
// height contraint to change the size.
//
self.dateHeightConstraint.constant = 162;
}
}
回答by Teffi
@LordParsley solution did the trick.
@LordParsley 解决方案成功了。
Just some additional details:
只是一些额外的细节:
I notice it occurs on iPhone 5 series and not on iPhone 6/6 plus with leading and trailing constraints. Apparently the bug only appears when its frame width is 320. Probably its a miscalculation of picker subviews that causes the overlaps. This is quite funny because Apple is the one who set the default value and yet they've oversaw the issue.
我注意到它出现在 iPhone 5 系列上,而不是出现在 iPhone 6/6 plus 上,带有前导和尾随约束。显然,该错误仅在其框架宽度为 320 时出现。可能是对选择器子视图的错误计算导致了重叠。这很有趣,因为 Apple 是设置默认值的人,但他们已经监督了这个问题。
Anyways I hope this gets resolved with iOS 9.1 which is now in beta.
无论如何,我希望现在处于测试阶段的 iOS 9.1 可以解决这个问题。
回答by Franken
I have same issue when running my app in iOS 9, my app using UIDatePicker in .xib file. I resolved this problem by add it in code behind:
我在 iOS 9 中运行我的应用程序时遇到同样的问题,我的应用程序在 .xib 文件中使用 UIDatePicker。我通过在后面的代码中添加它解决了这个问题:
UIDatePicker* picker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 44, 320, 162)];
[self.view addSubview:picker];
I think, this's problem with new font San Francisco (the font is big than Helvetica) and .xib file. Hope this help. Thank!
我认为,这是新字体 San Francisco(字体比 Helvetica 大)和 .xib 文件的问题。希望这有帮助。谢谢!
回答by John John
On iPhone 5S iOS 9.1 my month names are truncated when I display the UIDatePicker. I fixed this problem by setting a local property as follows:
在 iPhone 5S iOS 9.1 上,当我显示 UIDatePicker 时,我的月份名称会被截断。我通过如下设置本地属性解决了这个问题:
NSLocale *uk = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
NSCalendar *cal = [NSCalendar currentCalendar];
[cal setLocale:uk];
[_backdateDatePicker setCalendar:cal];