重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
iOS自定义UIDatePicker日期选择器视图 ,首先看一下效果图:
创新互联于2013年创立,先为陵川等服务建站,陵川等地企业,进行企业商务咨询服务。为陵川企业网站制作PC+手机+微官网三网同步一站式服务解决您的所有建站问题。下面贴上相关代码:
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]; //创建控件 [self creatControl]; } - (void)creatControl { //textField _dateTextField = [[UITextField alloc] initWithFrame:CGRectMake(mainW * 0.05, mainW * 0.72, mainW * 0.9, mainW * 0.12)]; _dateTextField.background = [UIImage imageNamed:@"textFieldBj"]; _dateTextField.textAlignment = NSTextAlignmentRight; _dateTextField.placeholder = @"请设置日期"; _dateTextField.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]; _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]; //日期选择器 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
另外有需要云服务器可以了解下创新互联建站www.cdcxhl.com,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。