UIDatePicker
月と年のみを選択するために必要です。クラスリファレンスドキュメントを確認しました。UIDatePicker
のように見えUIView
ます。UIPickerView
サブビューである可能性があり、それをつかむことができればコンポーネントを非表示にできると想像しました。しかし、いいえ。それは不可能でした。その場合、独自のカスタム ピッカーを作成する必要がありますか? 何か案は?
11 に答える
同じ効果を得るための解決策を次に示します。このコード スニペットを使用するには、"Indentity inspector" の "Custom class" 内の nib ファイルにUIPickerView
置き換える必要があります。CDatePickerViewEx
.h ファイル
#import <UIKit/UIKit.h>
@interface CDatePickerViewEx : UIPickerView <UIPickerViewDelegate, UIPickerViewDataSource>
@property (nonatomic, strong, readonly) NSDate *date;
-(void)selectToday;
@end
.m ファイル
#import "CDatePickerViewEx.h"
// Identifiers of components
#define MONTH ( 0 )
#define YEAR ( 1 )
// Identifies for component views
#define LABEL_TAG 43
@interface CDatePickerViewEx()
@property (nonatomic, strong) NSIndexPath *todayIndexPath;
@property (nonatomic, strong) NSArray *months;
@property (nonatomic, strong) NSArray *years;
-(NSArray *)nameOfYears;
-(NSArray *)nameOfMonths;
-(CGFloat)componentWidth;
-(UILabel *)labelForComponent:(NSInteger)component selected:(BOOL)selected;
-(NSString *)titleForRow:(NSInteger)row forComponent:(NSInteger)component;
-(NSIndexPath *)todayPath;
-(NSInteger)bigRowMonthCount;
-(NSInteger)bigRowYearCount;
-(NSString *)currentMonthName;
-(NSString *)currentYearName;
@end
@implementation CDatePickerViewEx
const NSInteger bigRowCount = 1000;
const NSInteger minYear = 2008;
const NSInteger maxYear = 2030;
const CGFloat rowHeight = 44.f;
const NSInteger numberOfComponents = 2;
@synthesize todayIndexPath;
@synthesize months;
@synthesize years = _years;
-(void)awakeFromNib
{
[super awakeFromNib];
self.months = [self nameOfMonths];
self.years = [self nameOfYears];
self.todayIndexPath = [self todayPath];
self.delegate = self;
self.dataSource = self;
[self selectToday];
}
-(NSDate *)date
{
NSInteger monthCount = [self.months count];
NSString *month = [self.months objectAtIndex:([self selectedRowInComponent:MONTH] % monthCount)];
NSInteger yearCount = [self.years count];
NSString *year = [self.years objectAtIndex:([self selectedRowInComponent:YEAR] % yearCount)];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; [formatter setDateFormat:@"MMMM:yyyy"];
NSDate *date = [formatter dateFromString:[NSString stringWithFormat:@"%@:%@", month, year]];
return date;
}
#pragma mark - UIPickerViewDelegate
-(CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component
{
return [self componentWidth];
}
-(UIView *)pickerView: (UIPickerView *)pickerView
viewForRow: (NSInteger)row
forComponent: (NSInteger)component
reusingView: (UIView *)view
{
BOOL selected = NO;
if(component == MONTH)
{
NSInteger monthCount = [self.months count];
NSString *monthName = [self.months objectAtIndex:(row % monthCount)];
NSString *currentMonthName = [self currentMonthName];
if([monthName isEqualToString:currentMonthName] == YES)
{
selected = YES;
}
}
else
{
NSInteger yearCount = [self.years count];
NSString *yearName = [self.years objectAtIndex:(row % yearCount)];
NSString *currenrYearName = [self currentYearName];
if([yearName isEqualToString:currenrYearName] == YES)
{
selected = YES;
}
}
UILabel *returnView = nil;
if(view.tag == LABEL_TAG)
{
returnView = (UILabel *)view;
}
else
{
returnView = [self labelForComponent: component selected: selected];
}
returnView.textColor = selected ? [UIColor blueColor] : [UIColor blackColor];
returnView.text = [self titleForRow:row forComponent:component];
return returnView;
}
-(CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
return rowHeight;
}
#pragma mark - UIPickerViewDataSource
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return numberOfComponents;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
if(component == MONTH)
{
return [self bigRowMonthCount];
}
return [self bigRowYearCount];
}
#pragma mark - Util
-(NSInteger)bigRowMonthCount
{
return [self.months count] * bigRowCount;
}
-(NSInteger)bigRowYearCount
{
return [self.years count] * bigRowCount;
}
-(CGFloat)componentWidth
{
return self.bounds.size.width / numberOfComponents;
}
-(NSString *)titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
if(component == MONTH)
{
NSInteger monthCount = [self.months count];
return [self.months objectAtIndex:(row % monthCount)];
}
NSInteger yearCount = [self.years count];
return [self.years objectAtIndex:(row % yearCount)];
}
-(UILabel *)labelForComponent:(NSInteger)component selected:(BOOL)selected
{
CGRect frame = CGRectMake(0.f, 0.f, [self componentWidth],rowHeight);
UILabel *label = [[UILabel alloc] initWithFrame:frame];
label.textAlignment = UITextAlignmentCenter;
label.backgroundColor = [UIColor clearColor];
label.textColor = selected ? [UIColor blueColor] : [UIColor blackColor];
label.font = [UIFont boldSystemFontOfSize:18.f];
label.userInteractionEnabled = NO;
label.tag = LABEL_TAG;
return label;
}
-(NSArray *)nameOfMonths
{
NSDateFormatter *dateFormatter = [NSDateFormatter new];
return [dateFormatter standaloneMonthSymbols];
}
-(NSArray *)nameOfYears
{
NSMutableArray *years = [NSMutableArray array];
for(NSInteger year = minYear; year <= maxYear; year++)
{
NSString *yearStr = [NSString stringWithFormat:@"%i", year];
[years addObject:yearStr];
}
return years;
}
-(void)selectToday
{
[self selectRow: self.todayIndexPath.row
inComponent: MONTH
animated: NO];
[self selectRow: self.todayIndexPath.section
inComponent: YEAR
animated: NO];
}
-(NSIndexPath *)todayPath // row - month ; section - year
{
CGFloat row = 0.f;
CGFloat section = 0.f;
NSString *month = [self currentMonthName];
NSString *year = [self currentYearName];
//set table on the middle
for(NSString *cellMonth in self.months)
{
if([cellMonth isEqualToString:month])
{
row = [self.months indexOfObject:cellMonth];
row = row + [self bigRowMonthCount] / 2;
break;
}
}
for(NSString *cellYear in self.years)
{
if([cellYear isEqualToString:year])
{
section = [self.years indexOfObject:cellYear];
section = section + [self bigRowYearCount] / 2;
break;
}
}
return [NSIndexPath indexPathForRow:row inSection:section];
}
-(NSString *)currentMonthName
{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"MMMM"];
return [formatter stringFromDate:[NSDate date]];
}
-(NSString *)currentYearName
{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy"];
return [formatter stringFromDate:[NSDate date]];
}
@end
ええ、おそらくあなたは自分のピッカーを作りたいと思うでしょう。ただし、サブクラス化する必要はありません。ジェネリックを使用して、 /メソッドUIPickerView
から適切な値を返すだけです。UIPickerViewDelegate
UIPickerViewDataSource
いいえ、ストックではできませんUIDatePicker
。
UIDatePickerModeDate
日付ピッカーには、月、日、および年が表示されます。これらの項目の正確な順序は、ロケール設定によって異なります。このモードの例は [ 11 月 | 11 月 ] です。15 | 2007]。
UIPickerView
をカスタマイズして 2 つのコンポーネントを使用し、その行に から取得した月と年の記号を入力することをお勧めしますNSDateFormatter
。
怠惰な回避策があります-10x10の黒いpngを作成しました。次に、それを、日付列を完全にカバーするサイズの scaletofill imageview を持つマスクとして使用しました。その列がほとんど表示されないように、アルファを 0.75 に設定しました。それはユーザーにとって明確であり、適切に見えます。
私はUIPickerView
2つのコンポーネント(月と年)を持つ単純なものを使用しています。
UIPickerViewDelegate
行を選択するたびにを呼び出します。
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
//Today year and month
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"yyyy"];
int year = [[dateFormat stringFromDate:[NSDate date]] intValue];
[dateFormat setDateFormat:@"MM"];
int month = [[dateFormat stringFromDate:[NSDate date]] intValue] - 1;
//picker year and month
int selectedYear = [[self.yearsArray objectAtIndex:[pickerView selectedRowInComponent:1]] intValue];
int selectedMonth = [pickerView selectedRowInComponent:0];
// if month in the past, change the month
if (year == selectedYear && selectedMonth < month)
{
[pickerView selectRow:month inComponent:0 animated:YES];
}
}
以下のように、値の変更イベントが発生したときに、日を月の最初にリセットします。そのため、ユーザーが日を選択すると、スクロールして 1 日目に戻ります。開始日(月と年)の選択には以下を使用します。
-(void) resetDay {
NSDate *currentDate = [startDatePicker date];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *componentsYear = [gregorian components:(NSYearCalendarUnit| NSMonthCalendarUnit) fromDate:currentDate];
NSInteger yearNum = [ componentsYear year];
NSInteger monthNum = [componentsYear month];
NSLog(@"Month %d, Year %d", monthNum, yearNum);
NSDateComponents *componentStartDate = [[NSDateComponents alloc] init];
[componentStartDate setDay:1];
[componentStartDate setMonth:monthNum];
[componentStartDate setYear:yearNum];
NSDate *startDate = [gregorian dateFromComponents:componentStartDate];
[startDatePicker setDate:startDate animated:YES];
[componentStartDate release];
[gregorian release];
}
終了日 (月と年) を選択するには、選択した月の 31 日または 30 日に日を設定したかったので、少し長くなります。
-(NSInteger) findDaysInYear {
NSDate *currentDate = [NSDate date];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *componentsYear = [gregorian components:(NSYearCalendarUnit) fromDate:currentDate];
NSInteger yearNum = [ componentsYear year];
NSDateComponents *componentStartDate = [[NSDateComponents alloc] init];
[componentStartDate setDay:1];
[componentStartDate setMonth:1];
[componentStartDate setYear:yearNum];
NSDate *startDate = [gregorian dateFromComponents:componentStartDate];
NSLog(@"Start date set to %@", startDate);
NSDateComponents *componentEndDate = [[NSDateComponents alloc] init];
[componentEndDate setDay:31];
[componentEndDate setMonth:12];
[componentEndDate setYear:yearNum];
NSDate *endDate = [gregorian dateFromComponents:componentEndDate];
NSLog(@"End date set to %@", endDate);
NSUInteger unitFlags = NSDayCalendarUnit ;
NSDateComponents *componentsDay = [gregorian components:unitFlags fromDate:startDate toDate:endDate options:0];
NSInteger days = [componentsDay day] +1;
NSLog(@"Number of days in the year:%d, is %d", yearNum,days);
[componentEndDate release];
[componentStartDate release];
[gregorian release];
if (days != 365 && days != 366) {
return 366;
}
return days;
}
-(NSInteger) findDaysInMonth:(NSInteger) monthNum {
NSInteger days = 30;
switch (monthNum) {
case 1:
days = 31;
break;
case 2:
if([self findDaysInYear] == 366) days = 29;
else days = 28;
break;
case 3:
days = 31;
break;
case 4:
days = 30;
break;
case 5:
days = 31;
break;
case 6:
days = 30;
break;
case 7:
days = 31;
break;
case 8:
days = 31;
break;
case 9:
days = 30;
break;
case 10:
days = 31;
break;
case 11:
days = 30;
break;
case 12:
days = 31;
break;
default:
break;
}
return days;
}
-(void) resetDay {
NSDate *currentDate = [endDatePicker date];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *componentsYear = [gregorian components:(NSYearCalendarUnit| NSMonthCalendarUnit) fromDate:currentDate];
NSInteger yearNum = [ componentsYear year];
NSInteger monthNum = [componentsYear month];
NSDateComponents *componentStartDate = [[NSDateComponents alloc] init];
[componentStartDate setDay:[self findDaysInMonth:monthNum]];
[componentStartDate setMonth:monthNum];
[componentStartDate setYear:yearNum];
NSDate *startDate = [gregorian dateFromComponents:componentStartDate];
[endDatePicker setDate:startDate animated:YES];
[componentStartDate release];
[gregorian release];
}
という名前のオープン ソース ライブラリを使用できます。
AKMonthYearPickerView
import AKMonthYearPickerView
AKMonthYearPickerView.sharedInstance.show(vc: viewController, doneHandler: doneHandler, completetionalHandler: completetionalHandler)
https://github.com/ali-cs/AKMonthYearPickerView
ポッドを使用してインストールするには、
pod 'AKMonthYearPickerView'
前年度の制限とバーの色をカスタマイズする
AKMonthYearPickerView.sharedInstance.barTintColor = UIColor.blue
AKMonthYearPickerView.sharedInstance.previousYear = 4
次のように、日の列を非常に簡単に非表示にすることができます。
datePicker.subviews[0].subviews[0].subviews[1].hidden = true
楽しみ :)