系統的UIPickerView很簡單,樣式也是很簡單單調,界面感覺很單調不怎麼好看,有時候就需要我們來自己自定義,做出自己想要的樣式。首先給出普通樣式的UIPickerView示例,貼上代碼:
#import "zidingyipikViewController.h"
@interface zidingyipikViewController ()<UIPickerViewDelegate,UIPickerViewDataSource>{
//記錄滾輪是否滑動
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