您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 使用技巧 >> iOS 7 技巧

iOS UIPickerView 使用與自訂樣式完整示例

編輯:iOS 7 技巧

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];
}

這裡有個實作細節值得學:guildStr 用來記錄「使用者有沒有真的動過滾輪」。若沒動過就採用第一筆資料作為結果,避免按下確定卻拿到空值。

二、自訂樣式:改字體、換分隔線

想更換外觀,只要改寫 viewForRow:forComponent:reusingView: 這個方法,回傳自訂的 view 即可。這個範例把文字改成 17pt 黑字、置中、透明背景,並把系統上下兩條黑線藏起來,換成自己畫的淺色線條。

//重寫方法,改變字體大小
- (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;
}

三、注意事項

隱藏系統分隔線是靠 pickerView.subviews 的索引硬取,這種做法在系統改版後有失效風險,正式專案建議改用「蓋一層同色 view 遮住」的替代方案會更穩。

另外常用到的螢幕尺寸巨集:

// 當前屏幕寬度
#define kScreenWidth [UIScreen mainScreen].bounds.size.width
// 當前屏幕高度
#define kScreenHeight [UIScreen mainScreen].bounds.size.height
  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved