蘋果選擇器严衬,還是比較方便使用的巡李,但是每次抚笔,UI設(shè)計(jì)師都會(huì)有自己的獨(dú)特見解,哈哈侨拦,有點(diǎn)意思殊橙。有一個(gè)開源的框架自定義程度還挺高的,好像是通過UITableView 來做的。還挺有意思的膨蛮,大家可以去看看螃概。傳送門 -- PGDatePicker
下面針對(duì)UIPickerView 做一些調(diào)整,先看效果圖鸽疾。
下面是實(shí)現(xiàn)代碼:Demo
原理:就是在復(fù)用的時(shí)候吊洼,進(jìn)行修改。和 UITableView 自定義Cell 等類似制肮。
- (UIView *)pickerView:(UIPickerView *)pickerView
viewForRow:(NSInteger)row forComponent:(NSInteger)component
reusingView:(UIView *)view{
//普通狀態(tài)的顏色
UILabel* norLabel = (UILabel*)view;
if (!norLabel){
norLabel = [[UILabel alloc] init];
norLabel.textColor = [UIColor grayColor];
norLabel.adjustsFontSizeToFitWidth = YES;
[norLabel setTextAlignment:NSTextAlignmentCenter];
[norLabel setBackgroundColor:[UIColor blueColor]];
[norLabel setFont:[UIFont systemFontOfSize:13]];
//icon
UIImageView *av = [[UIImageView alloc] initWithFrame:CGRectMake(30, 3, 20, 20)];
av.image = [UIImage imageNamed:[NSString stringWithFormat:@"img_%d", (int)row%2]];
[norLabel addSubview:av];
}
norLabel.text = [self pickerView:pickerView
titleForRow:row
forComponent:component];
//當(dāng)前選中的顏色
UILabel *selLb = (UILabel*)[pickerView viewForRow:row forComponent:0];
if (selLb) {
selLb.textColor = [UIColor brownColor];
selLb.adjustsFontSizeToFitWidth = YES;
[selLb setTextAlignment:NSTextAlignmentCenter];
[selLb setBackgroundColor:[UIColor purpleColor]];
[selLb setFont:[UIFont systemFontOfSize:16]];
}
//下一個(gè)選中的顏色(為了選中狀態(tài)不突兀冒窍,自己注釋看看效果)
UILabel *selLb1 = (UILabel*)[pickerView viewForRow:row + 1 forComponent:0];
if (selLb1) {
selLb1.textColor = [UIColor redColor];
selLb1.adjustsFontSizeToFitWidth = YES;
[selLb1 setTextAlignment:NSTextAlignmentCenter];
[selLb1 setBackgroundColor:[UIColor greenColor]];
[selLb1 setFont:[UIFont systemFontOfSize:16]];
}
//設(shè)置分割線
for (UIView *line in pickerView.subviews) {
if (line.frame.size.height < 1) {//0.6667
line.backgroundColor = [UIColor blackColor];
CGRect tempRect = line.frame;
CGFloat lineW = 120;
line.frame = CGRectMake((pickerView.frame.size.width - lineW) * 0.5, tempRect.origin.y, lineW, 2);
}
}
return norLabel;
}