2016-08-17 39 views
3

我增加了一個選擇器視圖和數據源,並委託像這樣:爲什麼UIPickerView顯示一個白色邊框

- (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; 
} 

但選擇器顯示是這樣的:

enter image description here

有一個白色邊框約150px在右側。

我秀行的邊框和選擇器的邊框調試

+0

你在做模擬器還是真實設備? –

+0

@Mike Alter我已經在模擬器和實際設備上嘗試過同樣的問題。 – jojoT

+0

使用此:http://stackoverflow.com/a/965421/4033273 –

回答

0

我已經找到了問題原因:我沒有設置選擇器視圖的以幀正確的價值觀。當我在 - (void)layoutSubviews中寫入框架設置代碼時,它在調用dataSource的方法之後調用,因此無法正確顯示。因此,我加上[self layoutIfNeed]在初始化後手動調用layoutSubviews,並解決了問題。

1

更新方法

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

謝謝,儘管它沒有解決問題,但您注意到pickerView.frame.width是導致問題的原因。我剛剛修復了它。 – jojoT