How iOS customizes the UIDatePicker date chooser view
This article mainly shows you "iOS how to customize UIDatePicker date selector view", the content is easy to understand, clear, hope to help you solve your doubts, the following let Xiaobian lead you to study and learn "iOS how to customize UIDatePicker date selector view" this article.
Code:
ViewController:
# import @ interface ViewController: UIViewController@end#import "ViewController.h" # import "HWDatePicker.h" # define mainW [UIScreen mainScreen] .bounds.size.width # define mainH [UIScreen mainScreen]. Bounds.size.height@interface ViewController () @ property (nonatomic, weak) HWDatePicker * datePicker;@property (nonatomic, strong) UITextField * dateTextField;@end@implementation ViewController- (void) viewDidLoad {[super viewDidLoad]; self.view.backgroundColor = [UIColor blackColor]; / / create control [self creatControl] }-(void) creatControl {/ / textField _ dateTextField = [[UITextField alloc] initWithFrame:CGRectMake (mainW * 0.05,0.72, mainW * 0.72, mainW * 0.9, mainW * 0.12)]; _ dateTextField.background = [UIImage imageNamed:@ "textFieldBj"]; _ dateTextField.textAlignment = NSTextAlignmentRight; _ dateTextField.placeholder = @ "Please set date"; _ dateTextField.delegate = self; UILabel * lab2 = [[UILabel alloc] initWithFrame:CGRectMake (0,0, mainW * 0.40, mainW * 0.12)] Lab2.textAlignment = NSTextAlignmentLeft; lab2.text = @ "date"; lab2.textColor = [UIColor grayColor]; _ dateTextField.leftView = lab2; _ dateTextField.leftViewMode = UITextFieldViewModeAlways; UILabel * lab22 = [[UILabel alloc] initWithFrame:CGRectMake (mainW * 0.12-15,0,15, mainW * 0.12)]; _ dateTextField.rightView = lab22; _ dateTextField.rightViewMode = UITextFieldViewModeAlways; [self.view addSubview:_dateTextField] / date selector HWDatePicker * datePicker = [[HWDatePicker alloc] initWithFrame:CGRectMake (mainW * 0.05, mainH, mainW * 0.9, mainW * 0.5)]; datePicker.delegate = self; [self.view addSubview:datePicker]; self.datePicker = datePicker;} # pragma mark-UITextFieldDelegate- (BOOL) textFieldShouldBeginEditing: (UITextField *) textField {if (_ datePicker.frame.origin.y! = mainH & & _ datePicker! = nil) {[_ datePicker dismiss]; return NO } else if (textField = = _ dateTextField) {[_ datePicker show]; return NO;} return YES;} # pragma mark-HWDatePickerDelegate- (void) datePickerView: (HWDatePicker *) datePickerView didClickSureBtnWithSelectDate: (NSString *) date {_ dateTextField.text = date;} @ end
HWDatePicker:
# import @ class HWDatePicker;@protocol HWDatePickerDelegate / * HWDatePicker confirm button Click Agent event * * @ param datePickerView HWDatePicker * @ param date selected date * /-(void) datePickerView: (HWDatePicker *) datePickerView didClickSureBtnWithSelectDate: (NSString *) date;@end@interface HWDatePicker: UIView@property (nonatomic, weak) id delegate;- (void) show;- (void) dismiss @ end#import "HWDatePicker.h" / / get the width and height of the screen # define mainW [UIScreen mainScreen] .bounds.size.width # define mainH [UIScreen mainScreen]. Bounds.size.height@interface HWDatePicker () @ property (nonatomic, strong) UIDatePicker * datePicker;@end@implementation HWDatePicker- (id) initWithFrame: (CGRect) frame {if (self = [super initWithFrame:frame]) {/ / background frame UIImageView * back = [UIImageView alloc] initWithFrame:CGRectMake (0,0, self.bounds.size.width, self.bounds.size.height)] Back.image = [UIImage imageNamed:@ "datePickerBj"]; [self addSubview:back]; / date selector _ datePicker = [[UIDatePicker alloc] init]; _ datePicker.frame = CGRectMake (10,10, self.frame.size.width-20,120); _ datePicker.backgroundColor = [UIColor clearColor]; [_ datePicker setDatePickerMode:UIDatePickerModeDate]; NSLocale * locale = [[NSLocale alloc] initWithLocaleIdentifier:@ "zh_CN"]; _ datePicker.locale = locale; NSDateFormatter * formatter_minDate = [NSDateFormatter alloc] init] [formatter_minDate setDateFormat:@ "yyyy-MM-dd"]; NSDate * minDate = [formatter_minDate dateFromString:@ "2008-01-01"]; formatter_minDate = nil; [_ datePicker setMinimumDate:minDate]; [self addSubview:_datePicker]; / / OK button 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) sureBtnOnClick {[self dismiss]; if (_ delegate & [_ delegate respondsToSelector:@selector (datePickerView:didClickSureBtnWithSelectDate:)]) {[_ delegate datePickerView:self didClickSureBtnWithSelectDate: [self getDateString]];}}-(NSString *) getDateString {NSDateFormatter * dateFormatter = [NSDateFormatter alloc] init] [dateFormatter setDateFormat:@ "yyyy-MM-dd"]; NSString * date = [dateFormatter stringFromDate: [self.datePicker date]]; return date;}-(void) show {[UIView animateWithDuration:0.3 animations: ^ {self.frame = CGRectMake (mainW * .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 is all the content of the article "how iOS customizes the UIDatePicker date Selector View". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!