精华内容
下载资源
问答
  • 自定义UIPickerView

    2015-12-23 09:04:33
    简单实现自定义UIPickerView 自定义UIPickerView

    简单实现自定义UIPickerView


    DEMO地址 :自定义UIPickerView



    展开全文
  • 自定义UIPickerView修改显示样式

    千次阅读 2016-09-23 00:11:09
    自定义UIPickerView修改显示样式
    //重写UIPickerViewDataSource中的这个方法
    - (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
        UILabel* pickerLabel = (UILabel*)view;
        if (!pickerLabel){
            pickerLabel = [[UILabel alloc] init];
            pickerLabel.adjustsFontSizeToFitWidth = YES;
            [pickerLabel setTextAlignment:NSTextAlignmentCenter];
            [pickerLabel setBackgroundColor:[UIColor clearColor]];
            [pickerLabel setFont:[UIFont boldSystemFontOfSize:16]];
        }
        // Fill the label text here
        pickerLabel.text=[self pickerView:pickerView titleForRow:row forComponent:component];
        return pickerLabel;
    }
    展开全文
  • iOS 自定义UIPickerView地区选择器视图。

    自定义UIPickerView地区选择器视图 ,首先看一下效果图:

    下面贴上相关代码:

    ViewController:

    #import <UIKit/UIKit.h>
    
    @interface ViewController : UIViewController
    
    @end
    
    
    #import "ViewController.h"
    #import "HWAreaPicker.h"
    
    #define mainW [UIScreen mainScreen].bounds.size.width
    #define mainH [UIScreen mainScreen].bounds.size.height
    
    @interface ViewController ()<UITextFieldDelegate, HWAreaPickerDelegate>
    
    @property (nonatomic, strong) HWAreaPicker *areaPicker;
    @property (nonatomic, strong) UITextField *areaTextField;
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        
        self.view.backgroundColor = [UIColor blackColor];
        
        //创建控件
        [self creatControl];
    }
    
    - (void)creatControl
    {
        //textField
        _areaTextField = [[UITextField alloc] initWithFrame:CGRectMake(mainW * 0.05, mainW * 0.72, mainW * 0.9, mainW * 0.12)];
        _areaTextField.background = [UIImage imageNamed:@"textFieldBj"];
        _areaTextField.textAlignment = NSTextAlignmentRight;
        _areaTextField.placeholder = @"请设置地区";
        _areaTextField.delegate = self;
        UILabel *lab2 = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, mainW * 0.4, mainW * 0.12)];
        lab2.textAlignment = NSTextAlignmentLeft;
        lab2.text = @"  地区";
        lab2.textColor = [UIColor grayColor];
        _areaTextField.leftView = lab2;
        _areaTextField.leftViewMode = UITextFieldViewModeAlways;
        UILabel *lab22 = [[UILabel alloc] initWithFrame:CGRectMake(mainW * 0.12 - 15, 0, 15, mainW * 0.12)];
        _areaTextField.rightView = lab22;
        _areaTextField.rightViewMode = UITextFieldViewModeAlways;
        [self.view addSubview:_areaTextField];
        
        //地区选择器
        HWAreaPicker *areaPicker = [[HWAreaPicker alloc] initWithFrame:CGRectMake(mainW * 0.05, mainH, mainW * 0.9, mainW * 0.5)];
        areaPicker.delegate = self;
        [self.view addSubview:areaPicker];
        self.areaPicker = areaPicker;
    }
    
    #pragma mark - UITextFieldDelegate
    - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
    {
        if (_areaPicker.frame.origin.y != mainH && _areaPicker != nil) {
            [_areaPicker dismiss];
            return NO;
            
        }else if (textField == _areaTextField) {
            [_areaPicker show];
            return NO;
        }
        
        return YES;
    }
    
    #pragma mark - HWAreaPickerDelegate
    - (void)didSelectRowInHWAreaPickerView:(HWAreaPicker *)areaPicker
    {
        _areaTextField.text = [NSString stringWithFormat:@"%@ %@ %@", areaPicker.locate.state, areaPicker.locate.city, areaPicker.locate.district];
    }
    
    @end

    HWAreaPicker:

    #import <UIKit/UIKit.h>
    
    @class HWAreaPicker, HWLocationModel;
    
    @protocol HWAreaPickerDelegate <NSObject>
    
    /**
     *  HWAreaPicker选中代理事件
     *
     *  @param areaPicker HWAreaPicker
     */
    - (void)didSelectRowInHWAreaPickerView:(HWAreaPicker *)areaPicker;
    
    @end
    
    @interface HWAreaPicker : UIView
    
    @property (nonatomic, strong) HWLocationModel *locate;
    @property (nonatomic, weak) id<HWAreaPickerDelegate> delegate;
    
    - (void)show;
    - (void)dismiss;
    
    @end
    
    
    #import "HWAreaPicker.h"
    
    //获得屏幕的宽高
    #define mainW [UIScreen mainScreen].bounds.size.width
    #define mainH [UIScreen mainScreen].bounds.size.height
    
    @interface HWAreaPicker ()<UIPickerViewDelegate, UIPickerViewDataSource>
    
    @property (nonatomic, strong) UIPickerView *areaPicker;
    @property (nonatomic, strong) NSArray *provinces;
    @property (nonatomic, strong) NSArray *cities;
    @property (nonatomic, strong) NSArray *areas;
    
    @end
    
    @implementation HWAreaPicker
    
    - (HWLocationModel *)locate
    {
        if (_locate == nil) {
            _locate = [[HWLocationModel alloc] init];
        }
        return _locate;
    }
    
    - (id)initWithFrame:(CGRect)frame
    {
        if (self = [super initWithFrame:frame]) {
            //背景框
            UIImageView *back = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, mainW * 0.9, mainW * 0.5)];
            back.image = [UIImage imageNamed:@"areaPickerBj"];
            [self addSubview:back];
            
            //地区选择器
            _areaPicker = [[UIPickerView alloc] init];
            _areaPicker.frame = CGRectMake(10, 10, self.frame.size.width - 20, 120);
            _areaPicker.delegate = self;
            _areaPicker.dataSource = self;
            [self addSubview:_areaPicker];
      
            _provinces = [[NSArray alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"area.plist" ofType:nil]];
            _cities = [[_provinces objectAtIndex:0] objectForKey:@"cities"];
            
            self.locate.state = [[_provinces objectAtIndex:0] objectForKey:@"state"];
            self.locate.city = [[_cities objectAtIndex:0] objectForKey:@"city"];
            
            _areas = [[_cities objectAtIndex:0] objectForKey:@"areas"];
            if (_areas.count > 0) {
                self.locate.district = [_areas objectAtIndex:0];
            } else{
                self.locate.district = @"";
            }
            
            //确定按钮
            UIButton *sureBtn = [[UIButton alloc] initWithFrame:CGRectMake((self.frame.size.width - mainW * 0.36) * 0.5, self.frame.size.height * 0.747, mainW * 0.36, mainW * 0.11)];
            [sureBtn setImage:[UIImage imageNamed:@"sureBtn"] forState:UIControlStateNormal];
            [sureBtn addTarget:self action:@selector(sureBtnOnClick) forControlEvents:UIControlEventTouchUpInside];
            [self addSubview:sureBtn];
        }
        return self;
    }
    
    #pragma mark - UIPickerViewDelegate
    - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
    {
        return 3;
    }
    
    - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
    {
        switch (component) {
            case 0:
                return [_provinces count];
                break;
            case 1:
                return [_cities count];
                break;
            case 2:
                return [_areas count];
                break;
            default:
                return 0;
                break;
        }
    }
    
    - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
    {
        switch (component) {
            case 0:
                return [[_provinces objectAtIndex:row] objectForKey:@"state"];
                break;
            case 1:
                return [[_cities objectAtIndex:row] objectForKey:@"city"];
                break;
            case 2:
                if ([_areas count] > 0) {
                    return [_areas objectAtIndex:row];
                    break;
                }
            default:
                return  @"";
                break;
        }
    }
    
    - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
    {
        switch (component) {
            case 0:
                _cities = [[_provinces objectAtIndex:row] objectForKey:@"cities"];
                [_areaPicker selectRow:0 inComponent:1 animated:YES];
                [_areaPicker reloadComponent:1];
                
                _areas = [[_cities objectAtIndex:0] objectForKey:@"areas"];
                [_areaPicker selectRow:0 inComponent:2 animated:YES];
                [_areaPicker reloadComponent:2];
                
                self.locate.state = [[_provinces objectAtIndex:row] objectForKey:@"state"];
                self.locate.city = [[_cities objectAtIndex:0] objectForKey:@"city"];
                if ([_areas count] > 0) {
                    self.locate.district = [_areas objectAtIndex:0];
                } else{
                    self.locate.district = @"";
                }
                break;
            case 1:
                _areas = [[_cities objectAtIndex:row] objectForKey:@"areas"];
                [_areaPicker selectRow:0 inComponent:2 animated:YES];
                [_areaPicker reloadComponent:2];
                
                self.locate.city = [[_cities objectAtIndex:row] objectForKey:@"city"];
                if ([_areas count] > 0) {
                    self.locate.district = [_areas objectAtIndex:0];
                } else{
                    self.locate.district = @"";
                }
                break;
            case 2:
                if ([_areas count] > 0) {
                    self.locate.district = [_areas objectAtIndex:row];
                } else{
                    self.locate.district = @"";
                }
                break;
            default:
                break;
        }
        
        if (_delegate && [_delegate respondsToSelector:@selector(didSelectRowInHWAreaPickerView:)]) {
            [_delegate didSelectRowInHWAreaPickerView:self];
        }
    }
    
    - (void)sureBtnOnClick
    {
        [self dismiss];
    }
    
    - (void)show
    {
        [UIView animateWithDuration:0.3 animations:^{
            self.frame = CGRectMake(mainW * 0.05, mainH - mainW * 0.75, mainW * 0.9, mainW * 0.5);
        }];
    }
    
    - (void)dismiss
    {
        [UIView animateWithDuration:0.3 animations:^{
            self.frame = CGRectMake(mainW * 0.05, mainH, mainW * 0.9, mainW * 0.5);
        }];
    }
    
    @end
    

    HWLocationModel:

    #import <Foundation/Foundation.h>
    
    @interface HWLocationModel : NSObject
    
    @property (copy, nonatomic) NSString *country;
    @property (copy, nonatomic) NSString *state;
    @property (copy, nonatomic) NSString *city;
    @property (copy, nonatomic) NSString *district;
    @property (copy, nonatomic) NSString *street;
    @property (nonatomic) double latitude;
    @property (nonatomic) double longitude;
    
    @end
    
    
    #import "HWLocationModel.h"
    
    @implementation HWLocationModel
    
    @synthesize country = _country;
    @synthesize state = _state;
    @synthesize city = _city;
    @synthesize district = _district;
    @synthesize street = _street;
    @synthesize latitude = _latitude;
    @synthesize longitude = _longitude;
    
    @end
    





    展开全文
  • iOS 自定义UIPickerView天数选择器视图。

    自定义UIPickerView天数选择器视图 ,首先看一下效果图:


    下面贴上相关代码:

    ViewController:

    #import <UIKit/UIKit.h>
    
    @interface ViewController : UIViewController
    
    @end
    
    
    #import "ViewController.h"
    #import "HWDaysPicker.h"
    
    #define mainW [UIScreen mainScreen].bounds.size.width
    #define mainH [UIScreen mainScreen].bounds.size.height
    
    @interface ViewController ()<UITextFieldDelegate, HWDaysPickerDelegate>
    
    @property (nonatomic, strong) HWDaysPicker *daysPicker;
    @property (nonatomic, strong) UITextField *daysTextField;
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        
        self.view.backgroundColor = [UIColor blackColor];
        
        //创建控件
        [self creatControl];
    }
    
    - (void)creatControl
    {
        //textField
        _daysTextField = [[UITextField alloc] initWithFrame:CGRectMake(mainW * 0.05, mainW * 0.72, mainW * 0.9, mainW * 0.12)];
        _daysTextField.background = [UIImage imageNamed:@"textFieldBj"];
        _daysTextField.textAlignment = NSTextAlignmentRight;
        _daysTextField.placeholder = @"请设置天数  ";
        _daysTextField.delegate = self;
        UILabel *lab2 = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, mainW * 0.4, mainW * 0.12)];
        lab2.textAlignment = NSTextAlignmentLeft;
        lab2.text = @"  天数";
        lab2.textColor = [UIColor grayColor];
        _daysTextField.leftView = lab2;
        _daysTextField.leftViewMode = UITextFieldViewModeAlways;
        UILabel *lab22 = [[UILabel alloc] initWithFrame:CGRectMake(mainW * 0.12 - 15, 0, 15, mainW * 0.12)];
        _daysTextField.rightView = lab22;
        _daysTextField.rightViewMode = UITextFieldViewModeAlways;
        [self.view addSubview:_daysTextField];
    }
    
    //天数选择器
    - (HWDaysPicker *)daysPicker
    {
        if (_daysPicker == nil) {
            _daysPicker = [[HWDaysPicker alloc] initWithFrame:CGRectMake(mainW * 0.05, mainH, mainW * 0.9, mainW * 0.5)];
            _daysPicker.delegate = self;
            [self.view addSubview:_daysPicker];
        }
        return _daysPicker;
    }
    
    #pragma mark - UITextFieldDelegate
    - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
    {
        if (_daysPicker.frame.origin.y != mainH && _daysPicker != nil) {
            [_daysPicker dismiss];
            return NO;
            
        }else if (textField == _daysTextField) {
            [self.daysPicker show];
            return NO;
        }
        
        return YES;
    }
    
    #pragma mark - HWDaysPickerDelegate
    - (void)daysPickerView:(HWDaysPicker *)daysPicker didSelectRowWithDays:(NSString *)days
    {
        _daysTextField.text = days;
    }
    
    @end
    

    HWDaysPicker:

    #import <UIKit/UIKit.h>
    
    @class HWDaysPicker;
    
    @protocol HWDaysPickerDelegate <NSObject>
    
    /**
     *  HWDaysPicker选中代理事件
     *
     *  @param daysPicker HWDaysPicker
     *  @param days       选中的天数
     */
    - (void)daysPickerView:(HWDaysPicker *)daysPicker didSelectRowWithDays:(NSString *)days;
    
    @end
    
    @interface HWDaysPicker : UIView
    
    @property (nonatomic, weak) id<HWDaysPickerDelegate> delegate;
    
    - (void)show;
    - (void)dismiss;
    
    @end
    
    
    #import "HWDaysPicker.h"
    
    #define mainW [UIScreen mainScreen].bounds.size.width
    #define mainH [UIScreen mainScreen].bounds.size.height
    
    @interface HWDaysPicker ()<UIPickerViewDataSource, UIPickerViewDelegate>
    
    @property (nonatomic, strong) UIPickerView *daysPicker;
    @property (nonatomic, strong) NSArray *daysArray;
    
    @end
    
    @implementation HWDaysPicker
    
    - (id)initWithFrame:(CGRect)frame
    {
        if (self = [super initWithFrame:frame]) {
            //背景框
            UIImageView *back = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height)];
            back.image = [UIImage imageNamed:@"daysPickerBj"];
            [self addSubview:back];
            
            //天数选择器(这里用initWithFrame会导致pickerView高度为系统默认高度,无法改变)
            _daysPicker = [[UIPickerView alloc] init];
            _daysPicker.frame = CGRectMake(10, 10, self.frame.size.width - 20, 120);
            _daysPicker.delegate = self;
            _daysPicker.dataSource = self;
            [self addSubview:_daysPicker];
            
            _daysArray = @[@"20", @"21", @"22", @"23", @"24", @"25", @"26", @"27", @"28", @"29", @"30", @"31", @"32", @"33", @"34", @"35", @"36", @"37", @"38", @"39", @"40", @"41", @"42", @"43", @"44", @"45"];
            
            //确定按钮
            UIButton *sureBtn = [[UIButton alloc] initWithFrame:CGRectMake((self.frame.size.width - mainW * 0.36) * 0.5, self.frame.size.height * 0.747, mainW * 0.36, mainW * 0.11)];
            [sureBtn setImage:[UIImage imageNamed:@"sureBtn"] forState:UIControlStateNormal];
            [sureBtn addTarget:self action:@selector(sureBtnOnClick) forControlEvents:UIControlEventTouchUpInside];
            [self addSubview:sureBtn];
        }
        
        return self;
    }
    
    - (void)layoutSubviews
    {
        [super layoutSubviews];
        
        //初始化位置
        [_daysPicker selectRow:8 inComponent:0 animated:NO];
        [self pickerView:_daysPicker didSelectRow:8 inComponent:0];
    }
    
    #pragma mark - UIPickerViewDelegate
    - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
    {
        return 1;
    }
    
    - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
    {
        return _daysArray.count;
    }
    
    - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
    {
        return _daysArray[row];
    }
    
    - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
    {
        if (_delegate && [_delegate respondsToSelector:@selector(daysPickerView:didSelectRowWithDays:)]) {
            [_delegate daysPickerView:self didSelectRowWithDays:_daysArray[row]];
        }
    }
    
    - (void)sureBtnOnClick
    {
        [self dismiss];
    }
    
    - (void)show
    {
        [UIView animateWithDuration:0.3f animations:^{
            self.frame = CGRectMake(mainW * 0.05, mainH - mainW * 0.75, mainW * 0.9, mainW * 0.5);
        }];
    }
    
    - (void)dismiss
    {
        [UIView animateWithDuration:0.3f animations:^{
            self.frame = CGRectMake(mainW * 0.05, mainH, mainW * 0.9, mainW * 0.5);
        }];
    }
    
    @end
    




    展开全文
  • 自定义的UIPikerView,可以供各种项目使用,里面内容可以更改,将类拖进工程即可,需设置代理,有两个代理方法,一个是确定按钮,一个是取消按钮
  • 需要重写UIPickerView中的 1 -(UIView*)pickerView:(UIPickerView*)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view... 1 //自定义uipickerview中的文字大小 ...
  • ios iphone 自定义UIPickerView

    千次阅读 2011-12-15 03:04:51
    在自己定义UIPickerView的时候,遇到的各种问题,小小总结一下。 1、获取当前的时间: NSDate *date = [NSDate date]; NSDateFormatter *monthFormatter = [[[NSDateFormatter alloc] init] autorelease]; ...
  • UIPickerView是iOS开发中,相当常用的一个UI控件,用于滚动选择选项。也是项目中经常复用的一个控件,封装成一个统一风格的库,可以减少很多代码量。一般还会在PickerView上加上Toolbar和确定取消按钮。点击button弹...
  • 在实际开发中很多时候需要下拉列表类似ui显示数据通常最好方案是使用UIPickerView,但是单纯的去使用UIPickerView不美观用户体验也差,所以我们需要自己对UIPickerView进行包装。下面是对UIPickerView包装的实现。 ...
  • 自定义iOS UIPickerView

    2020-07-11 03:04:52
    In this tutorial, we’ll be customising the UIPickerView properties in our iOS Application. In the previous tutorial, we implemented the UIPickerView class and discussed some of the important helper ...
  • 自定义UIPickerView

    2011-08-11 17:25:08
    UIPickerView 没有subviews 所以不能通过它的子view替换背景利用CAlayer 帮你搞定,你也可以随意的改变 UIPickerView的大小CGFloat color[4] = {1,1,0,1}; CGColorSpaceRef rgb = CGColorSpaceCreateDeviceRGB(); ...
  • 在使用UIPickerView显示时间,城市等的选择时,系统定义的样式总是与自己的页面不搭配需要进行精加工,就给大家介绍一下怎样自定义UIPickerView选中的字体颜色、字号、字体等属性 自定义UIPickerView选中的字体颜色...
  • 支持iOS8及以上,LCAlertViewController将 UIPickerView和UIAlertViewController结合, 并且添加自定义的View GITHub地址: https://github.com/LuochuanAD/LCAlertViewController 二,UIPickerView 与...
  • UIPickerView 自定义

    2013-11-27 16:19:01
    UIPickerView *pickerView = [[UIPickerView alloc]initWithFrame:CGRectMake(0, 0, 320, 320)]; pickerView.delegate = self; pickerView.dataSource = self; pickerView.showsSelectionIndicator = YES; ...
  • IOS自定义弹出UIPickerView或UIDatePicker(动画效果)
  • 写项目时,产品希望实现一个在运动里自定义运动时间的界面 天大地大产品最大,因为之前也对其他UI控件有所了解,所以我一下子锁定了UIDatePicker,可大概写了一个上去后发现原来还有更初级的UIViewPicker可以利用 ...
  • UIPickerView自定义背景

    2013-07-25 11:26:00
    #import <...@interface MyPicker : UIPickerView {}@end -------------------------------------------------------------------------------- //// MyPicker.m// PickerSkinTest//// Create...
  • UIPickerView允许开发者对列表项进行任意定制 开发者只要实现UIPickerViewDelegate协议中的-pickerView:viewForRow:forComponent: ...就是说自定义它的时候在方法中就行,因为它直接继承自uiview,UIPickerView...
  • - (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view 方法中自定义的时候,发现不能居中: 在经过一番探索之后,由...
  • - (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view { UILabel *lblDate = [[UILabel alloc] init]; [lblDate setFont:...
  • 前面iOS学习之UIPickerView控件的简单使用 用到的UIPickerView弹出来是通过 textField.inputView = selectPicker; textField.inputAccessoryView = doneToolbar; 这中方法来做的。如果UIPickerView或UIDatePicker...
  • 近日在写一个UIPickerView的时候,自定义视图可是发现不能居中:  在一番探索后,发现问题关键在label的frame上,修改了数值后就可以居中了,不过不是很明白原理:

空空如也

空空如也

1 2 3 4 5 ... 9
收藏数 178
精华内容 71
关键字:

自定义uipickerview