I have created custom TimePicker using UIPickerView
, taken 3 UIPickerView
.
- One for Hours
- One for Minutes
- One for AM/PM
By picking time, I am using that time to set local notification. Because my application is based on alarm functionality.
UI part and Functional part works well in iPhone 5,5s,6,6 Plus but facing issue in iPhone 6s plus device. Not getting what is going wrong! Same code works well in all other devices except iPhone 6s plus.
Below screenshot is of iPhone 6,
As you can see, ScreenShot time and PickerView time is same. It is functional part. I have written code like, catch the current time and as per that I have set the PickerView's selected index. Works perfect! Also I have set selected index fonts bigger. Which is also looks perfect in iPhone 6 device.
Below screenshot is of iPhone 6s Plus,
As you can see, ScreenShot time and PickerView
time is not same. So functional part is not working in iPhone 6s plus device. Also in UI part, Fonts getting bigger for selected row in second PickerView
but not in the first one.
Also when I set the local notification by picking time from time picker. It also works well (Notification gets fire on perfect time) in all other devices other than iPhone 6s plus. And in iPhone 6s plus whatever time I select from UIPickerView
, notification gets fire immediately.
Don't know what is going wrong with iPhone 6s plus device. Either issue is with PickerView or something else. But why the same code works well for all other device and not only for iPhone 6s plus device.
Any help is appreciated! Badly stuck in this issue. Please help if anyone have any solution for this issue! Thanks in advance.
UPDATE :
Please have a look in to my code...
AddAlarmViewController.h
@interface AddAlarmViewController : UIViewController <UIPickerViewDelegate,UIPickerViewDataSource>
{
}
@property (strong,nonatomic) NSArray *arrHours;
@property (strong,nonatomic) NSArray *arrMinute;
@property (strong,nonatomic) NSArray *arrAP;
@property (nonatomic,strong) IBOutlet UIPickerView *myPickerHour;
@property (nonatomic,strong) IBOutlet UIPickerView *myPickerMinute;
@property (nonatomic,strong) IBOutlet UIPickerView *myPickerAP;
AddAlarmViewController.m
@interface AddAlarmViewController ()
{
int countH,countM,countAP;
}
@property (strong,nonatomic) NSString *strTime,*strTimeSlot;
@property (strong,nonatomic) NSDate *dateSelected;
@property (assign,nonatomic) NSInteger rowHour,rowAP,rowMinute;
@property (strong,nonatomic) UILabel *lblHour,*lblMinute,*lblAP;
@end
@implementation AddAlarmViewController
- (void)viewDidLoad {
[super viewDidLoad];
countH = 0;
countM = 0;
countAP = 0;
_arrHours = @[[NSNumber numberWithInt:1],[NSNumber numberWithInt:2],[NSNumber numberWithInt:3],[NSNumber numberWithInt:4],[NSNumber numberWithInt:5],[NSNumber numberWithInt:6],[NSNumber numberWithInt:7],[NSNumber numberWithInt:8],[NSNumber numberWithInt:9],[NSNumber numberWithInt:10],[NSNumber numberWithInt:11],[NSNumber numberWithInt:12]];
_arrMinute = @[[NSNumber numberWithInt:0],[NSNumber numberWithInt:1],[NSNumber numberWithInt:2],[NSNumber numberWithInt:3],[NSNumber numberWithInt:4],[NSNumber numberWithInt:5],[NSNumber numberWithInt:6],[NSNumber numberWithInt:7],[NSNumber numberWithInt:8],[NSNumber numberWithInt:9],[NSNumber numberWithInt:10],[NSNumber numberWithInt:11],[NSNumber numberWithInt:12],[NSNumber numberWithInt:13],[NSNumber numberWithInt:14],[NSNumber numberWithInt:15],[NSNumber numberWithInt:16],[NSNumber numberWithInt:17],[NSNumber numberWithInt:18],[NSNumber numberWithInt:19],[NSNumber numberWithInt:20],[NSNumber numberWithInt:21],[NSNumber numberWithInt:22],[NSNumber numberWithInt:23],[NSNumber numberWithInt:24],[NSNumber numberWithInt:25],[NSNumber numberWithInt:26],[NSNumber numberWithInt:27],[NSNumber numberWithInt:28],[NSNumber numberWithInt:29],[NSNumber numberWithInt:30],[NSNumber numberWithInt:31],[NSNumber numberWithInt:32],[NSNumber numberWithInt:33],[NSNumber numberWithInt:34],[NSNumber numberWithInt:35],[NSNumber numberWithInt:36],[NSNumber numberWithInt:37],[NSNumber numberWithInt:38],[NSNumber numberWithInt:39],[NSNumber numberWithInt:40],[NSNumber numberWithInt:41],[NSNumber numberWithInt:42],[NSNumber numberWithInt:43],[NSNumber numberWithInt:44],[NSNumber numberWithInt:45],[NSNumber numberWithInt:46],[NSNumber numberWithInt:47],[NSNumber numberWithInt:48],[NSNumber numberWithInt:49],[NSNumber numberWithInt:50],[NSNumber numberWithInt:51],[NSNumber numberWithInt:52],[NSNumber numberWithInt:53],[NSNumber numberWithInt:54],[NSNumber numberWithInt:55],[NSNumber numberWithInt:56],[NSNumber numberWithInt:57],[NSNumber numberWithInt:58],[NSNumber numberWithInt:59]];
_arrAP = @[@"AM",@"PM"];
[self.myPickerHour reloadAllComponents];
[self.myPickerHour selectRow:_rowHour inComponent:0 animated:YES];
[self.myPickerMinute reloadAllComponents];
[self.myPickerMinute selectRow:_rowMinute inComponent:0 animated:YES];
[self.myPickerAP reloadAllComponents];
[self.myPickerAP selectRow:_rowAP inComponent:0 animated:YES];
}
#pragma mark - Picker View Data source
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
return 1;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView
numberOfRowsInComponent:(NSInteger)component{
if (pickerView.tag == 1) {
return [_arrHours count] * 100;
} else if (pickerView.tag == 2) {
return [_arrMinute count] * 100;
} else {
return [_arrAP count];
}
}
#pragma mark- Picker View Delegate
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:
(NSInteger)row inComponent:(NSInteger)component {
if (pickerView.tag == 1) {
NSLog(@" Row Number %ld", row % [_arrHours count]);
_lblHour = (UILabel*)[pickerView viewForRow:row forComponent:component];
[_lblHour setFont:[UIFont fontWithName:@"OpenSans-Light" size:70.0]];
[_lblHour setTextColor:[UIColor blackColor]];
NSLog(@"Print selected label of hour : %@",_lblHour.text);
if (countH == 4) {
isHour = TRUE;
//------------------Above and Below Rows------------------//
if (row > 0) {
UILabel *lbl = (UILabel *)[pickerView viewForRow:row-1 forComponent:component];
[lbl setFont:[UIFont fontWithName:@"OpenSans-Light" size:16.0]];
lbl.textAlignment = NSTextAlignmentCenter;
lbl.textColor = [UIColor darkGrayColor];
}
if (row < (_arrHours.count * 100)) {
UILabel *lbl1 = (UILabel *)[pickerView viewForRow:row+1 forComponent:component];
[lbl1 setFont:[UIFont fontWithName:@"OpenSans-Light" size:16.0]];
lbl1.textAlignment = NSTextAlignmentCenter;
lbl1.textColor = [UIColor darkGrayColor];
}
//-------------------------------------------------------//
}
} else if (pickerView.tag == 2) {
_lblMinute = (UILabel*)[pickerView viewForRow:row forComponent:component];
[_lblMinute setFont:[UIFont fontWithName:@"OpenSans-Light" size:70.0]];
[_lblMinute setTextColor:[UIColor blackColor]];
NSLog(@"Print selected label of minute : %@",_lblMinute.text);
if (countM == 4) {
isMinute = TRUE;
//------------------Above and Below Rows------------------//
if (row > 0) {
UILabel *lbl = (UILabel *)[pickerView viewForRow:row-1 forComponent:component];
[lbl setFont:[UIFont fontWithName:@"OpenSans-Light" size:16.0]];
lbl.textAlignment = NSTextAlignmentCenter;
lbl.textColor = [UIColor darkGrayColor];
}
if (row < (_arrMinute.count * 100)) {
UILabel *lbl1 = (UILabel *)[pickerView viewForRow:row+1 forComponent:component];
[lbl1 setFont:[UIFont fontWithName:@"OpenSans-Light" size:16.0]];
lbl1.textAlignment = NSTextAlignmentCenter;
lbl1.textColor = [UIColor darkGrayColor];
}
//-------------------------------------------------------//
}
} else if (pickerView.tag == 3) {
_lblAP = (UILabel*)[pickerView viewForRow:row forComponent:component];
[_lblAP setTextColor:[UIColor blackColor]];
NSLog(@"Print selected label of AP : %@",_lblAP.text);
}
NSLog(@"----------4");
NSDateFormatter *format = [[NSDateFormatter alloc] init];
[format setDateFormat:@"dd/MM/yyyy"];
NSString *str = [format stringFromDate:[NSDate date]];
NSDate *now = [NSDate date];
int daysToAdd = 1;
NSDate *newDate1 = [now dateByAddingTimeInterval:60*60*24*daysToAdd];
[format setDateFormat:@"hh:mm a"];
NSString *strHM = [format stringFromDate:now];
NSString *str1 = [NSString stringWithFormat:@"%@:%@ %@",_lblHour.text,_lblMinute.text,_lblAP.text];
NSString *strSelected = [NSString stringWithFormat:@"%@:%@ %@",_lblHour.text,_lblMinute.text,_lblAP.text];
if ([strHM isEqualToString:strSelected]) {
[format setDateFormat:@"dd/MM/yyyy"];
NSString *strWithOneDay = [format stringFromDate:newDate1];
_strTime = [NSString stringWithFormat:@"%@ %@",strWithOneDay,str1];
} else {
_strTime = [NSString stringWithFormat:@"%@ %@",str,str1];
}
[format setDateFormat:@"dd/MM/yyyy hh:mm a"];
_dateSelected = [format dateFromString:_strTime];
if (!(_dateSelected == NULL)) {
NSDateComponents *components = [[NSCalendar currentCalendar] components:NSCalendarUnitHour | NSCalendarUnitMinute fromDate:_dateSelected];
NSInteger hour = [components hour];
if(hour >= 3 && hour < 12) {
_strTimeSlot = @"GOOD MORNING";
}
else if(hour >= 12 && hour < 17) {
_strTimeSlot = @"GOOD AFTERNOON";
}
else if(hour >= 17 && hour < 20) {
_strTimeSlot = @"GOOD EVENING";
} else {
_strTimeSlot = @"GOOD NIGHT";
}
}
}
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
if (pickerView.tag == 1 || pickerView.tag == 2) {
return 70.0;
}
return 30.0;
}
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row
forComponent:(NSInteger)component reusingView:(UIView *)view
{
UILabel *lblTitle = (id)view;
if (pickerView.tag == 1) {
[[pickerView.subviews objectAtIndex:1] setHidden:TRUE];
[[pickerView.subviews objectAtIndex:2] setHidden:TRUE];
lblTitle= [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, [pickerView rowSizeForComponent:component].width, [pickerView rowSizeForComponent:component].height)];
[lblTitle setFont:[UIFont fontWithName:@"OpenSans-Light" size:16.0]];
lblTitle.textAlignment = NSTextAlignmentCenter;
lblTitle.textColor = [UIColor darkGrayColor];
if ([[NSString stringWithFormat:@"%@",[_arrHours objectAtIndex:(row % [_arrHours count])]] length] == 1) {
NSString *str = [@"0" stringByAppendingString:[NSString stringWithFormat:@"%@",[_arrHours objectAtIndex:(row % [_arrHours count])]]];
lblTitle.text = str;
} else {
lblTitle.text = [NSString stringWithFormat:@"%@",[_arrHours objectAtIndex:(row % [_arrHours count])]];
}
if (row == _rowHour && !isHour) {
countH++;
[self pickerView:self.myPickerHour didSelectRow:_rowHour inComponent:0];
_lblHour.text = lblTitle.text;
}
} else if (pickerView.tag == 2) {
[[pickerView.subviews objectAtIndex:1] setHidden:TRUE];
[[pickerView.subviews objectAtIndex:2] setHidden:TRUE];
lblTitle= [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, [pickerView rowSizeForComponent:component].width, [pickerView rowSizeForComponent:component].height)];
[lblTitle setFont:[UIFont fontWithName:@"OpenSans-Light" size:16.0]];
lblTitle.textAlignment = NSTextAlignmentCenter;
lblTitle.textColor = [UIColor darkGrayColor];
if ([[NSString stringWithFormat:@"%@",[_arrMinute objectAtIndex:(row % [_arrMinute count])]] length] == 1) {
NSString *str = [@"0" stringByAppendingString:[NSString stringWithFormat:@"%@",[_arrMinute objectAtIndex:(row % [_arrMinute count])]]];
lblTitle.text = str;
} else {
lblTitle.text = [NSString stringWithFormat:@"%@",[_arrMinute objectAtIndex:(row % [_arrMinute count])]];
}
if (row == _rowMinute && !isMinute) {
countM++;
[self pickerView:self.myPickerMinute didSelectRow:_rowMinute inComponent:0];
_lblMinute.text = lblTitle.text;
}
} else {
[[pickerView.subviews objectAtIndex:1] setHidden:TRUE];
[[pickerView.subviews objectAtIndex:2] setHidden:TRUE];
lblTitle= [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, [pickerView rowSizeForComponent:component].width, [pickerView rowSizeForComponent:component].height)];
[lblTitle setFont:[UIFont fontWithName:@"OpenSans-Light" size:16.0]];
lblTitle.textAlignment = NSTextAlignmentCenter;
lblTitle.textColor = [UIColor blackColor];
lblTitle.text = [_arrAP objectAtIndex:row];
if (row == _rowAP) {
[self pickerView:self.myPickerAP didSelectRow:_rowAP inComponent:0];
_lblAP.text = lblTitle.text;
}
}
return lblTitle;
}