苹果选择器,还是比较方便使用的,但是每次,UI设计师都会有自己的独特见解,哈哈,有点意思。有一个开源的框架自定义程度还挺高的,好像是通过UITableView 来做的。还挺有意思的,大家可以去看看。传送门 -- PGDatePicker
下面针对UIPickerView 做一些调整,先看效果图。
效果图
下面是实现代码:Demo
原理:就是在复用的时候,进行修改。和 UITableView 自定义Cell 等类似。
- (UIView *)pickerView:(UIPickerView *)pickerView
viewForRow:(NSInteger)row forComponent:(NSInteger)component
reusingView:(UIView *)view{
//普通状态的颜色
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];
//当前选中的颜色
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]];
}
//下一个选中的颜色(为了选中状态不突兀,自己注释看看效果)
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]];
}
//设置分割线
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;
}
网友评论