ios開發中UIPickerView使用例子

來源:互聯網
上載者:User

系統的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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.