移动应用软件开发综合实训(基于iOS平台)-2017 自定义选择器设计 iOS自定义选择器.docxVIP

  • 1
  • 0
  • 约3.27万字
  • 约 48页
  • 2021-01-15 发布于北京
  • 举报

移动应用软件开发综合实训(基于iOS平台)-2017 自定义选择器设计 iOS自定义选择器.docx

iOS自定义选择器 一、概述 如果需求和iOS内置的选择器样式都不符合的话,那么就没办法使用UIPicker和UIDatePicker,比如当只需要显示年、月信息的时候,显然UIDatePicker没办法满足我们的需求,那这时我们只能通过UIPickerView来自定义自己想要的选择器。 二、UIPickerView的自定义使用 (1)创建基类继承于UIView的WXZBasePickView。 我们常见的选择器的样式是一个带透明背景色的view,底部是内容的选择器,有确定和取消按钮,大致如图: 选择器 所以我们创建一个基类view,这个view的样式如图所示样式,之后根据内容的差别创建基于该view的选择器。 在.h中声明各个属性及方法 #import UIKit/UIKit.h @interface WXZBasePickView : UIView @property (nonatomic, strong) UIView *contentView; //选择器 @property (nonatomic, strong)UIPickerView *pickerView; //取消按钮 @property (nonatomic, strong)UIButton *cancelButton; //确定按钮 @property (nonatomic, strong)UIButton *confirmButton; //选择器每一列的高度 @property (nonatomic, assign)CGFloat pickerViewHeight; /** * 创建视图,初始化视图时初始数据 */ - (void)initPickView; /** * 确认按钮的点击事件 */ - (void)clickConfirmButton; /** * pickerView的显示 */ - (void)show; /** * 移除pickerView */ - (void)disMiss; @end 在.m中实现相关方法 #import WXZBasePickView.h #define ScreenWidth [UIScreen mainScreen].bounds.size.width #define ScreenHeight [UIScreen mainScreen].bounds.size.height @implementation WXZBasePickView - (instancetype)init { self = [super init]; if (self) { _pickerViewHeight = 250; self.bounds = [UIScreen mainScreen].bounds; self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.64]; self.layer.opacity = 0.0; UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(disMiss)]; self.userInteractionEnabled = YES; [self addGestureRecognizer:tap]; [self addSubview:self.contentView]; [self.contentView addSubview:self.pickerView]; [self.contentView addSubview:self.cancelButton]; [self.contentView addSubview:self.confirmButton]; [self initPickView]; } return self; } //初始化选择器内容,创建子类时需实现该父类方法 -(void)initPickView{ } //点击确定按钮 - (void)clickConfirmButton { [self disMiss]; } //点击取消按钮 - (void) clickCancelButton { [self disMiss]

您可能关注的文档

文档评论(0)

1亿VIP精品文档

相关文档