我正在尝试从https://github.com/christopherney/FlatDatePicker自定义FlatDatePicker。
我的目标是从日期选择器的顶部删除两个按钮,并在屏幕底部添加一个确认按钮。在原始的FlatDatePicker.m文件中,作者使用以下代码初始化datepicker,tile和按钮:
if ([self initWithFrame:CGRectMake(0.0, _parentView.frame.size.height, _parentView.frame.size.width, kFlatDatePickerHeight)]) {
_datePickerMode = FlatDatePickerModeDate;
[_parentView addSubview:self];
[self setupControl];
}
return self;
_labelTitle = [[UILabel alloc] initWithFrame:CGRectMake(kFlatDatePickerButtonHeaderWidth, 0.0, self.frame.size.width - (kFlatDatePickerHeaderHeight * 2), kFlatDatePickerHeaderHeight)];
_buttonValid = [[UIButton alloc] initWithFrame:CGRectMake(self.frame.size.width - kFlatDatePickerButtonHeaderWidth, 0.0, kFlatDatePickerButtonHeaderWidth, kFlatDatePickerHeaderHeight)];
然后将调用show方法从屏幕上向上滑动日期选择器:
-(void)show {
if (_parentView != nil) {
if (self.hidden == YES) {
self.hidden = NO;
}
if (_isInitialized == NO) {
self.frame = CGRectMake(self.frame.origin.x, _parentView.frame.size.height, self.frame.size.width, self.frame.size.height);
_isInitialized = YES;
}
if (self.datePickerMode == FlatDatePickerModeDate ||
self.datePickerMode == FlatDatePickerModeDateAndTime ||
self.datePickerMode == FlatDatePickerModeMonthAndYear) {
if ( self.datePickerMode != FlatDatePickerModeMonthAndYear ) {
int indexDays = [self getIndexForScrollViewPosition:_scollViewDays];
[self highlightLabelInArray:_labelsDays atIndex:indexDays];
}
int indexMonths = [self getIndexForScrollViewPosition:_scollViewMonths];
[self highlightLabelInArray:_labelsMonths atIndex:indexMonths];
int indexYears = [self getIndexForScrollViewPosition:_scollViewYears];
[self highlightLabelInArray:_labelsYears atIndex:indexYears];
}
if (self.datePickerMode == FlatDatePickerModeTime || self.datePickerMode == FlatDatePickerModeDateAndTime) {
int indexHours = [self getIndexForScrollViewPosition:_scollViewHours];
[self highlightLabelInArray:_labelsHours atIndex:indexHours];
int indexMinutes = [self getIndexForScrollViewPosition:_scollViewMinutes];
[self highlightLabelInArray:_labelsMinutes atIndex:indexMinutes];
int indexSeconds = [self getIndexForScrollViewPosition:_scollViewSeconds];
[self highlightLabelInArray:_labelsSeconds atIndex:indexSeconds];
}
if (IS_PHONE) {
[UIView beginAnimations:@"FlatDatePickerShow" context:nil];
[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:kFlatDatePickerAnimationDuration];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(animationShowDidFinish)];
}
self.frame = CGRectMake(self.frame.origin.x, _parentView.frame.size.height - kFlatDatePickerHeight, self.frame.size.width, self.frame.size.height);
if (IS_PHONE) [UIView commitAnimations];
}
}
由于按钮和标题的初始y坐标设置为0.0,而datepicker的初始y坐标是_parentView.frame.size.height,这是屏幕的底部,因此在运行之后它们是如何相互靠近的“show”方法???
另外,当我尝试使用以下代码在datepicker正下方添加一个确认按钮时,还有另一个问题:
_buttonValid = [[UIButton alloc] initWithFrame:CGRectMake(0.0, self.frame.size.height, self.frame.size.width, kFlatDatePickerHeaderHeight)];
[self addSubview:_buttonValid]
运行后,按钮被添加到正确的位置,但按下时没有响应(在按钮的位置发生变化之前从未发生过)。