1. 程式人生 > >UIPickerView內容居中顯示,設定初始預設值

UIPickerView內容居中顯示,設定初始預設值

系統預設的是內容居左顯示的,有時會覺得不太好看,可以實現代理方法。

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view

{
    UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(12.0f, 0.0f, [pickerView rowSizeForComponent:component].width-12, [pickerView rowSizeForComponent:component].height)] autorelease];

    [label setText:[pickerData objectAtIndex:row]];

    label.backgroundColor = [UIColor clearColor];
    [label setTextAlignment:UITextAlignmentCenter];
    return label;
}

設定初始預設值

[pickerView selectRow:3 inComponent:0 animated:NO];