1. 程式人生 > >iOS10的UIPickerView分割線顏色,文字顏色

iOS10的UIPickerView分割線顏色,文字顏色

遵循代理,然後實現下面的代理方法即可:

   pickerView.delegate = self;

   pickerView.dataSource = self;

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView*)pickerView

{

return1;

}

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component

{

return40;

}

- (NSInteger

)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component

{

returnself.genderArray.count;

}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow: (NSInteger)row inComponent:(NSInteger)component

{

_genderStr = [NSStringstringWithFormat:@"%@",self.genderArray[row]];

//獲取選中的文字,以便於在別的地方使用

}


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

{

    //設定分割線的顏色

for(UIView *singleLine in pickerView.subviews)

    {

if (singleLine.frame.size.height < 1)

        {

            singleLine.

backgroundColor = kSingleLineColor;

        }

    }

    //設定文字的屬性

UILabel *genderLabel = [UILabelnew];

    genderLabel.textAlignment = NSTextAlignmentCenter;

    genderLabel.text = self.genderArray[row];//self.genderArray裡邊內容為@[@"男",@"女"]

    genderLabel.textColor = kSpecialTextColor;

return genderLabel;

}