先设置代理,再写代理方法,
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
return NO;
}
pragma mark - 导航栏设置
-(void)navigationBarConfiguration{
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake((kScreenWidth-140)/2.0, 64-30-10, 140, 30)];
textField.textColor = [UIColor blackColor];
textField.textAlignment = NSTextAlignmentCenter;
textField.leftViewMode = UITextFieldViewModeAlways;
textField.rightViewMode = UITextFieldViewModeAlways;
textField.tag = 20;
textField.delegate = self;
textField.text = [NSString stringWithFormat:@"%lu年%lu月",[self yearInDate:self.today],[self monthInDate:self.today]];
UIImage *leftImage = [UIImage imageNamed:@"left_img"];
UIImageView *leftImgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, leftImage.size.width, leftImage.size.height)];
leftImgView.image = leftImage;
leftImgView.userInteractionEnabled = YES;
UITapGestureRecognizer *leftImgViewGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(navigaImgViewTouched:)];
[leftImgView addGestureRecognizer:leftImgViewGesture];
textField.leftView = leftImgView;
UIImage *rightImage = [UIImage imageNamed:@"right_img"];
UIImageView *rightImgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, rightImage.size.width, rightImage.size.height)];
rightImgView.image = rightImage;
rightImgView.userInteractionEnabled = YES;
UITapGestureRecognizer *rightImgViewGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(navigaImgViewTouched:)];
[rightImgView addGestureRecognizer:rightImgViewGesture];
textField.rightView = rightImgView;
[self.view addSubview:textField];
}
pragma mark - 日期切换
-(void)navigaImgViewTouched:(UITapGestureRecognizer *)gesture{
UITextField *textField = (UITextField *)[self.view viewWithTag:20];
UIScrollView *scrollView = (UIScrollView *)[self.view viewWithTag:100];
if ([gesture.view isEqual:textField.leftView]) {//前面的月份
//下面的方法不能用setContentOffset:方法替换,否则不会调用scrollViewDidEndScrollingAnimation方法;使用下面的方法时,不会调用scrollViewDidEndDecelerating方法,所以需要在scrollViewDidEndScrollingAnimation方法中调用scrollViewDidEndDecelerating方法,从而实现相关的逻辑
[scrollView setContentOffset:CGPointMake(-kScreenWidth, 0) animated:YES];
}else{//后面的月份
[scrollView setContentOffset:CGPointMake(kScreenWidth*2, 0) animated:YES];
}
}