Detail the iOS time selection box
- 2020-05-17 06:36:31
- OfStack
This article introduces the iOS time selection box sample code for your reference, the specific content is as follows
Code:
1. The header files
#import <UIKit/UIKit.h>
@class ITTPickView;
@protocol ITTPickViewDelegate <NSObject>
@optional
-(void)toobarDonBtnHaveClick:(ITTPickView *)pickView
resultString:(NSString *)resultString;
@end
@interface ITTPickView : UIView
@property(nonatomic,weak) id<ITTPickViewDelegate> delegate;// entrust
/**
* Create by time 1 a DatePicker
*
* @param date Default selection time
* @param isHaveNavControler Whether in NavControler within
*
* @return with toolbar the datePicker
*/
-(instancetype)initDatePickWithDate:(NSDate *)defaulDate
datePickerMode:(UIDatePickerMode)datePickerMode
isHaveNavControler:(BOOL)isHaveNavControler;
/**
* Removes this control from the window
*/
-(void)removeView;
/**
* Displays the control in a window
*/
-(void)showView;
@end
2. Implementation of ITTPickView, main controls UIToolBar, UIDatePicker, click ok and execute -(void)toobarDonBtnHaveClick:(ITTPickView *)
pickView resultString:(NSString *)resultString (because it is an optional delegate event, the implementation will execute) ; Gets the selected time string.
#import "ITTPickView.h"
#define ITTToobarHeight 40
@interface ITTPickView ()
@property (nonatomic,assign) NSDate *defaulDate;// The default time
@property (nonatomic,strong) UIDatePicker *datePicker;//datePicker controls
@property (nonatomic,assign) NSInteger pickeviewHeight;//pickerView The height of the
@property (nonatomic,strong) UIToolbar *toolbar;//toolBar controls
@property (nonatomic,copy) NSString *resultString;// The time string returned
@property (nonatomic,assign) NSInteger selfOriginy;// The current view the frame.origin.y
@property (nonatomic,assign) NSInteger selfViewInitH;// The initial state view the frame.origin.y
@end
@implementation ITTPickView
// Initialize the 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;
}
// set ITTPickView the frame The size of the
-(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;// The initial state view the frame.origin.y
self.selfOriginy = toolViewY;// The current view the frame.origin.y
self.frame = CGRectMake(toolViewX, toolViewInitH, toolViewW, toolViewH);
}
// set datePicker Control style as well frame Size and as view The child views
-(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];
}
// Set up the toolBar The various properties of and as view The child views
- (void)setUpToolBar {
self.toolbar = [self setToolbarStyle];
[self setToolbarWithPickViewFrame];
[self addSubview:self.toolbar];
}
// Set up the toolBar The style of the
-(UIToolbar *)setToolbarStyle {
UIToolbar *toolbar = [[UIToolbar alloc] init];
UIBarButtonItem *left = [[UIBarButtonItem alloc] initWithTitle:@" cancel " style:UIBarButtonItemStylePlain target:self action:@selector(removeView)];
UIBarButtonItem *centerSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
UIBarButtonItem *right = [[UIBarButtonItem alloc] initWithTitle:@" determine " style:UIBarButtonItemStylePlain target:self action:@selector(doneClick)];
toolbar.items = @[lefttem, centerSpace, right];
return toolbar;
}
// set tooBar the frame The size of the
- (void)setToolbarWithPickViewFrame {
self.toolbar.frame = CGRectMake(0, 0,[UIScreen mainScreen].bounds.size.width, ITTToobarHeight);
}
// Click ok
-(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];
}
/**
* Removes this control from the window
*/
- (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];
}
}];
}
/**
* Displays the control in a window
*/
- (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
3. Use the ITTPickView
UIButton *testBitton = [[UIButton alloc] initWithFrame:CGRectMake(0, 450, 111, 40)];
[testBitton setBackgroundColor:[UIColor redColor]];
[testBitton addTarget:self action:@selector(test) forControlEvents:UIControlEventTouchUpInside];
// Click the button to bring up the time selection box
- (void)test {
ITTPickView *datePicker = [[ITTPickView alloc] initDatePickWithDate:[NSDate date] datePickerMode:UIDatePickerModeDate isHaveNavControler:NO];
[datePicker showView];
}
The above is the entire content of this article, I hope to help you with your study.