【问题标题】:UIPickerView Maximum Width in iOS 9iOS 9 中的 UIPickerView 最大宽度
【发布时间】:2015-11-02 22:15:13
【问题描述】:

我有一个应用程序,它使用 UIPickerView 设置与图形系列的线型有关的各种参数(线宽、颜色和线型)。此选择器用于表视图行。我已经为选择器定义了框架,以便它的宽度填充单元格的 contentView 成员的宽度。直到 iOS 9,这似乎工作。在 iOS 9 中,UIPickerView 的宽度似乎有某种最大上限。有没有人遇到过类似的情况?

选择器视图是这样创建的:

self.picker = [[[UIPickerView alloc] init] autorelease];
[self.contentView addSubview:self.picker];
self.picker.dataSource = self;
self.picker.delegate = self;
self.picker.backgroundColor = [UIColor grouped_table_view_background_colour];

单元格布局如下:

-(void) layoutSubviews
{
   // we need to allow the base class to perform its layout.
   static CGFloat left_margin = 40;
   static CGFloat right_margin = 40;
   CGSize my_size;
   [super layoutSubviews];
   my_size = self.contentView.bounds.size;
   my_size.width -= left_margin + right_margin;

   // we now need to lay out the views.
   CGRect picker_rect = CGRectMake(left_margin, 5, my_size.width, my_size.height);
   self.picker.frame = picker_rect;

   // we want to look at the bounds of the picker
   CGRect picker_bounds = self.picker.bounds;
   NSLog(@"picker bounds x=%g, y=%g, w=%g, h=%g",  picker_bounds.origin.x, picker_bounds.origin.y, picker_bounds.size.width, picker_bounds.size.height);
}

我也重载了 widthForComponent 方法,如下:

-(CGFloat) pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component
{
   // we want to look at the bounds of the picker
   CGRect picker_bounds = self.picker.bounds;
   NSLog(@"picker bounds x=%g, y=%g, w=%g, h=%g", picker_bounds.origin.x, picker_bounds.origin.y, picker_bounds.size.width, picker_bounds.size.height);
   return picker_bounds.size.width / 3;
}

我可以看到视图的边界在记录的值以及视图的背景颜色中都正确显示。尽管如此,选择器似乎填充不到可用宽度的一半(我的 iPad 上为 688 点)

【问题讨论】:

  • 我仍然不知道是什么原因造成的,但我已经找到了使用三个选择器视图而不是一个包含三个组件的选择器视图的解决方法。

标签: ios uitableview ios9 uipickerview


【解决方案1】:

[[UIPickerView alloc] init] 之后,Picker 的大小为 {320,216},此大小将用于pickerView:widthForComponent:。如果之后只是调整选取器的大小,则不会再次调用函数pickerView:widthForComponent:,因此组件会卡在它们的宽度上。

尝试使用正确的大小初始化选择器,例如.. picker=[[UIDatePicker new] initWithFrame:CGRectMake(0.0, 0.0, CGRectGetWidth(self.view.bounds), 216.0)];

或在更改选择器框架后调用[self.picker setNeedsLayout] 以强制再次调用pickerView:widthForComponent:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-05-14
    • 1970-01-01
    • 2012-11-13
    • 2013-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-06
    相关资源
    最近更新 更多