我在我的應用中使用了UIDatePicker
和UIPickerView
。但是這兩個控件都顯示出怪異的行爲。當我在選取器中滾動內容時,它會在選取器外面流動,並且不會按照應有的順序滾動。我的實現如下。UIPickerView內容意外流動
sexArray = @[@"Male", @"Female"];
UIPickerView *picker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 50, 100, 150)];
[picker setDataSource: self];
[picker setDelegate: self];
picker.showsSelectionIndicator = YES;
genderTF.inputView = picker;
#pragma mark -- UIPICKERVIEW METHODS
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
return sexArray.count;
}
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
return 1;
}
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row
forComponent:(NSInteger)component reusingView:(UIView *)view {
UILabel *retval = (UILabel*)view;
if (!retval) {
retval = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 100, [pickerView rowSizeForComponent:component].height)];
}
retval.font = [UIFont fontWithName:@"SourceSansPro-Regular" size:15.0f];
retval.minimumScaleFactor = 0.6;
[retval setTextAlignment:NSTextAlignmentCenter];
retval.text = [sexArray objectAtIndex:row];
return retval;
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
[genderTF setText:sexArray[component]];
}
當在另一個應用程序中使用相同的代碼(用於測試)時,它按預期工作。
retval = [[UILabel alloc] initWithFrame:CGRectMake(0.0f,0.0f,100,[pickerView rowSizeForComponent:component] .height)]; 到 retval = [[UILabel alloc] initWithFrame:CGRectMake(0.0f,0.0f,self.view.frame.size.width,[pickerView rowSizeForComponent:component] .height)]; –
@rkmakwana我認爲'font size'或'minimumScaleFactor'可能會影響到這一點。我認爲字體大小高於標籤高度。嘗試爲標籤設置背景顏色 –