系统的UIPickerView很简单,样式也是很简单单调,界面感觉很单调不怎么好看,有时候就需要我们来自己自定义,做出自己想要的样式。首先给出普通样式的UIPickerView示例,贴上代码:
#import "zidingyipikViewController.h"
@interface zidingyipikViewController ()
//记录滚轮是否滑动
NSString *guildStr;
NSString *selectStr;
NSMutableArray *dataMutArray;
UIButton *bgButton;
}
@end
@implementation zidingyipikViewController
- (void)viewDidLoad {
[super viewDidLoad];
dataMutArray = [NSMutableArray arrayWithArray:@[@"学生",@"工人",@"教师",@"保安",@"医生",@"护士",@"服务员"]];
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(80, 140, 70, 30)];
[button setTitle:@"弹出框" forState:UIControlStateNormal];
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
button.backgroundColor = [UIColor orangeColor];
[button addTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
#pragma mark - 弹框
- (void)buttonAction{
guildStr = @"0";
bgButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
bgButton.backgroundColor = RGBA(0, 0, 0, 0.3);
[bgButton addTarget:self action:@selector(bgButtonAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:bgButton];
UIView *cycanView = [[UIView alloc] initWithFrame:CGRectMake(0, kScreenHeight - 180, kScreenWidth, 40)];
cycanView.backgroundColor = [UIColor orangeColor];
[bgButton addSubview:cycanView];
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, cycanView.height)];
titleLabel.text = @"选择身份";
titleLabel.font = FONT(14);
titleLabel.textColor = [UIColor whiteColor];
titleLabel.textAlignment = NSTextAlignmentCenter;
[cycanView addSubview:titleLabel];
UIButton *cancelButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 48, cycanView.height)];
[cancelButton setTitle:@"取消" forState:UIControlStateNormal];
[cancelButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
cancelButton.titleLabel.font = FONT(14);
[cancelButton addTarget:self action:@selector(bgButtonAction:) forControlEvents:UIControlEventTouchUpInside];
[cycanView addSubview:cancelButton];
UIButton *confirmButton = [[UIButton alloc] initWithFrame:CGRectMake(kScreenWidth - 48, 0, 48, cycanView.height)];
[confirmButton setTitle:@"确定" forState:UIControlStateNormal];
[confirmButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
confirmButton.titleLabel.font = FONT(14);
[confirmButton addTarget:self action:@selector(confirmButtonAction:) forControlEvents:UIControlEventTouchUpInside];
[cycanView addSubview:confirmButton];
UIPickerView *selectPickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, kScreenHeight - 140, kScreenWidth, 140)];
// 显示选中框
selectPickerView.showsSelectionIndicator = NO;
selectPickerView.backgroundColor = [UIColor whiteColor];
selectPickerView.delegate = self;
selectPickerView.dataSource = self;
selectPickerView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
[bgButton addSubview:selectPickerView];
}
#pragma mark - 隐藏弹框
- (void)bgButtonAction:(UIButton *)sender{
[bgButton removeFromSuperview];
}
#pragma mark - 弹框确定按钮
- (void)confirmButtonAction:(UIButton *)sender{
if ([guildStr isEqualToString:@"0"]) {
selectStr = [NSString stringWithFormat:@"%@",dataMutArray[0]];
}
[self showHUDTextOnly:selectStr];
[bgButton removeFromSuperview];
}
#pragma mark - UIPickerView代理方法
// pickerView 列数
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 1;
}
// pickerView 每列个数
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
return dataMutArray.count;
}
// 每列宽度
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
return kScreenWidth - 85 * 2;
}
// 返回选中的行
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
guildStr = @"1";
selectStr = [NSString stringWithFormat:@"%@",[dataMutArray objectAtIndex:row]];
NSLog(@"selectStr:%@",selectStr);
}
//返回当前行的内容,此处是将数组中数值添加到滚动的那个显示栏上
-(NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return [dataMutArray objectAtIndex:row];
}
接下来,如果要改变样式,自定义自己需要的UIPickerView样式,只要重写方法就行了。方法:
//重写方法,改变字体大小
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
UILabel *pickerLabel = (UILabel *)view;
if (!pickerLabel){
pickerLabel = [[UILabel alloc] init];
pickerLabel.font = FONT(17);
pickerLabel.textColor = [UIColor blackColor];
pickerLabel.textAlignment = 1;
[pickerLabel setBackgroundColor:[UIColor clearColor]];
}
pickerLabel.text = [self pickerView:pickerView titleForRow:row forComponent:component];
//在该代理方法里添加以下两行代码删掉上下的黑线
[[pickerView.subviews objectAtIndex:1] setHidden:YES];
[[pickerView.subviews objectAtIndex:2] setHidden:YES];
UIView *lineView1 = [[UIView alloc] initWithFrame:CGRectMake(85, 55, kScreenWidth - 85 * 2, 1.8)];
lineView1.backgroundColor = RGB(245, 245, 245);
[pickerView addSubview:lineView1];
UIView *lineView2 = [[UIView alloc] initWithFrame:CGRectMake(85, 82, kScreenWidth - 85 * 2, 1.8)];
lineView2.backgroundColor = RGB(245, 245, 245);
[pickerView addSubview:lineView2];
return pickerLabel;
}
注:
// 当前屏幕宽度
#define kScreenWidth [UIScreen mainScreen].bounds.size.width
// 当前屏幕高度
#define kScreenHeight [UIScreen mainScreen].bounds.size.height
荒野乱斗国际版 (BrawlStars)最新版本v56.274
下载一波超人内置修改器菜单版 安卓版v1.0.2
下载敢达决战官方正版 安卓版v6.7.9
下载敢达决战 安卓版v6.7.9
下载继承了一座戏园子无限声望铜钱版 内置菜单最新版v1.7
继承了一座戏园子折相思版是游戏的破解版本,在该版本中为玩家提
山河半世橙光清软金手指版 无限鲜花v3.24
山河半世是一款超级好玩的橙光恋爱游戏,在游戏中玩家们需要扮演
蓬莱手游折相思版 安卓版v1.0.0
蓬莱免广告版是游戏的修改版本,在该版本中为玩家去除了广告,玩
当红影后橙光游戏破解版2025 最新版v1.0
当红影后橙光破解版是一款超级好玩的娱乐圈题材的橙光游戏,在这
忽然成了万人迷清软完结版 无限鲜花版v12.15
忽然成了万人迷破解版是一款非常好玩的男性向橙光游戏,在有一天