时间:2021-05-19
本文实例为大家介绍了iOS时间选择框的示例代码,供大家参考,具体内容如下
代码:
一、头文件
二、ITTPickView的实现,主要的控件UIToolBar、UIDatePicker,点击确定后执行-(void)toobarDonBtnHaveClick:(ITTPickView *)
pickView resultString:(NSString *)resultString(因为是可选的委托事件,实现了才会执行);获得选择的时间字符串。#import "ITTPickView.h"#define ITTToobarHeight 40@interface ITTPickView ()@property (nonatomic,assign) NSDate *defaulDate;//默认时间@property (nonatomic,strong) UIDatePicker *datePicker;//datePicker控件@property (nonatomic,assign) NSInteger pickeviewHeight;//pickerView的高度@property (nonatomic,strong) UIToolbar *toolbar;//toolBar控件@property (nonatomic,copy) NSString *resultString;//返回的时间字符串@property (nonatomic,assign) NSInteger selfOriginy;//当前view的frame.origin.y@property (nonatomic,assign) NSInteger selfViewInitH;//初始状态view的frame.origin.y@end@implementation ITTPickView//初始化ITTPickView,- (instancetype)initDatePickWithDate:(NSDate *)defaulDate datePickerMode:(UIDatePickerMode)datePickerMode isHaveNavControler:(BOOL)isHaveNavControler {self = [super init];if (self) {self.defaulDate = defaulDate;[self setUpDatePickerWithdatePickerMode:datePickerMode];[self setFrameWith:isHaveNavControler];[self setUpToolBar];}return self;}//设定ITTPickView的frame大小-(void)setFrameWith:(BOOL)isHaveNavControler {CGFloat toolViewX = 0;CGFloat toolViewH = self.pickeviewHeight + ITTToobarHeight;CGFloat toolViewY;if (isHaveNavControler) {toolViewY = [UIScreen mainScreen].bounds.size.height - toolViewH - 50;}else {toolViewY = [UIScreen mainScreen].bounds.size.height - toolViewH;}CGFloat toolViewW = [UIScreen mainScreen].bounds.size.width;CGFloat toolViewInitH = [UIScreen mainScreen].bounds.size.height;self.selfViewInitH = toolViewInitH;//初始状态view的frame.origin.yself.selfOriginy = toolViewY;//当前view的frame.origin.yself.frame = CGRectMake(toolViewX, toolViewInitH, toolViewW, toolViewH);}//设定datePicker控件的样式以及frame大小,并作为view的子视图-(void)setUpDatePickerWithdatePickerMode:(UIDatePickerMode)datePickerMode {UIDatePicker *datePicker = [[UIDatePicker alloc] init];datePicker.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh-CN"];datePicker.datePickerMode = datePickerMode;datePicker.backgroundColor = [UIColor whiteColor];if (self.defaulDate) {[datePicker setDate:self.defaulDate];}self.datePicker = datePicker;datePicker.frame = CGRectMake(0, ITTToobarHeight, [UIScreen mainScreen].bounds.size.width, datePicker.frame.size.height);self.pickeviewHeight = datePicker.frame.size.height;[self addSubview:datePicker];}//设置toolBar的各个属性,并作为view的子视图- (void)setUpToolBar {self.toolbar = [self setToolbarStyle];[self setToolbarWithPickViewFrame];[self addSubview:self.toolbar];}//设置toolBar的样式-(UIToolbar *)setToolbarStyle {UIToolbar *toolbar = [[UIToolbar alloc] init];UIBarButtonItem *left = [[UIBarButtonItem alloc] initWithTitle:@" 取消 " style:UIBarButtonItemStylePlain target:self action:@selector(removeView)];UIBarButtonItem *centerSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];UIBarButtonItem *right = [[UIBarButtonItem alloc] initWithTitle:@" 确定 " style:UIBarButtonItemStylePlain target:self action:@selector(doneClick)];toolbar.items = @[lefttem, centerSpace, right];return toolbar;}//设定tooBar的frame大小- (void)setToolbarWithPickViewFrame {self.toolbar.frame = CGRectMake(0, 0,[UIScreen mainScreen].bounds.size.width, ITTToobarHeight);}//点击确定按钮-(void)doneClick {if (self.datePicker) {NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];dateFormatter.dateFormat = @"yyyy-MM-dd";self.resultString = [dateFormatter stringFromDate:self.datePicker.date];}if ([self.delegate respondsToSelector:@selector(toobarDonBtnHaveClick:resultString:)]) {[self.delegate toobarDonBtnHaveClick:self resultString:self.resultString];}[self removeView];}/*** 从窗口移除本控件*/- (void)removeView {[UIView animateWithDuration:0.25f animations:^{self.frame = CGRectMake(self.frame.origin.x, self.selfViewInitH, self.frame.size.width, self.frame.size.height);} completion:^(BOOL finished) {if (finished) {[self removeFromSuperview];}}];}/*** 在窗口显示本控件*/- (void)showView {[[UIApplication sharedApplication].keyWindow addSubview:self];[UIView animateWithDuration:0.25f animations:^{self.frame = CGRectMake(self.frame.origin.x, self.selfOriginy, self.frame.size.width, self.frame.size.height);} completion:^(BOOL finished) {}];}@end三、运用ITTPickView
以上就是本文的全部内容,希望对大家的学习有所帮助。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
IOS贝塞尔曲线详解开发IOS的朋友都知道IOS贝塞尔曲线的重要性,由于经常会用到这样的东西,索性抽时间就把相应所有的属性,方法做一个总结。UIBezierPa
ReactNative日期时间选择组件:react-native-datepicker,支持安卓和IOS双平台,支持单独选择日期、单独选择时间和选择日期和时间,
IOS开发UIAlertController详解在iOS8.0后,苹果弃用了UIAlertView和UIActionSheet,转而使用UIAlertContr
下面通过实例代码给大家分享5种android对话框,具体内容详情如下所示:1弹出普通对话框---系统更新2自定义对话框--用户登录3时间选择对话框--时间对话框
IOS给xcode工程关联pod的实例详解1.新建Podfile文件内容如下:platform:ios,'7.0'target:LJMediaPalyerdop