Search code examples
iosobjective-ciphoneuipickerview

Why UIPickerView shows with a white border


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


Solution

  • 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.