3

I added a picker view and the dataSource and delegate like this:

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return _datas.count;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    return _datas[component].count;
}

- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
CGFloat width = SCREEN_W / _datas.count;
    return width;
}

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component {
    return kActionPickerRowHeight;
}


- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
    UILabel *label = [[UILabel alloc] init];
    label.frame = CGRectMake(0, 0, SCREEN_W / _datas.count, kActionPickerRowHeight);
    label.textAlignment = NSTextAlignmentCenter;
    label.text =  [NSString stringWithFormat:@"%@",_datas[component][row]];
    label.font = [UIFont systemFontOfSize:12.f];

    label.layer.borderWidth = 1.f;
    return label;
}

But the picker display like this:

enter image description here

There is a white border about 150px at the right side.

I show the row's border and picker's border to debug

kb920
  • 3,039
  • 2
  • 33
  • 44
jojoT
  • 153
  • 15
  • are you doing in simulator or real device ? – Prashant Tukadiya Aug 17 '16 at 11:58
  • @Mike Alter I've tried in both simulator and real device, same issue. – jojoT Aug 17 '16 at 12:04
  • use this: http://stackoverflow.com/a/965421/4033273 – Moin Shirazi Aug 17 '16 at 12:17
  • what is the value of SCREEN_W – Aruna Mudnoor Aug 17 '16 at 12:19
  • It is issue with setting picker view width. Update your method with given answer by @ArunAmmannaya. It can be a possible solution for your question. – Nirmit Dagly Aug 17 '16 at 12:28
  • I 've found what cause the problem : I didn't set the picker view 's frame to correct values in time. As I write the frame-setting code in `- (void)layoutSubviews`, and it was called after the dataSource's methods called, so it couldn't display correctly. Thus, i plus `[self layoutIfNeed]`to call layoutSubviews manually after initialization, and problem solved. – jojoT Aug 17 '16 at 12:42

2 Answers2

1

Update method to

- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
CGFloat width = pickerView.frame.width / _datas.count;
    return width;
}
Aruna Mudnoor
  • 4,795
  • 14
  • 16
  • Thank you, even though It doesn't solve the problem, you notice me that pickerView.frame.width is what cause the problem. And i've fixed it just now. – jojoT Aug 17 '16 at 12:34
0

I 've found what cause the problem : I didn't set the picker view 's frame to correct values in time. As I write the frame-setting code in - (void)layoutSubviews‌​, and it was called after the dataSource's methods called, so it couldn't display correctly. Thus, i plus [self layoutIfNeed]to call layoutSubviews manually after initialization, and problem solved.

jojoT
  • 153
  • 15