简体   繁体   English

iPhone 6s plus设备中的UIPickerView UI和功能问题

[英]UIPickerView UI and Functional issue in iPhone 6s plus device

I have created custom TimePicker using UIPickerView , taken 3 UIPickerView . 我使用UIPickerView创建了自定义TimePicker,并使用了3 UIPickerView

  • One for Hours 一小时
  • One for Minutes 一分钟
  • One for AM/PM 一个用于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. UI部分和功能部分在iPhone 5,5s,6,6 Plus上运行良好,但在iPhone 6s plus设备上面临问题。 Not getting what is going wrong! 没有出什么问题! Same code works well in all other devices except iPhone 6s plus. 相同的代码在iPhone 6s plus以外的所有其他设备上均可正常运行。

Below screenshot is of iPhone 6, 下面的屏幕截图是iPhone 6,

在此处输入图片说明

As you can see, ScreenShot time and PickerView time is same. 如您所见,ScreenShot时间和PickerView时间是相同的。 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. 我已经编写了类似捕获当前时间的代码,并且按照我设置了PickerView的选定索引的要求编写了代码。 Works perfect! 完美的作品! Also I have set selected index fonts bigger. 我还设置了更大的索引字体。 Which is also looks perfect in iPhone 6 device. 在iPhone 6设备中看起来也很完美。

Below screenshot is of iPhone 6s Plus, 下面的屏幕截图是iPhone 6s Plus的,

在此处输入图片说明

As you can see, ScreenShot time and PickerView time is not same. 如您所见,ScreenShot时间和PickerView时间不同。 So functional part is not working in iPhone 6s plus device. 因此,功能部分在iPhone 6s plus设备中不起作用。 Also in UI part, Fonts getting bigger for selected row in second PickerView but not in the first one. 同样在UI部分中,第二个PickerView选定行的字体PickerView但第一个中的字体不会变大。

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. 它在iPhone 6s plus以外的所有其他设备上也能很好地工作(通知会在完美的时间触发)。 And in iPhone 6s plus whatever time I select from UIPickerView , notification gets fire immediately. 在iPhone 6s以及我从UIPickerView选择的任何时间,通知都会立即触发。

Don't know what is going wrong with iPhone 6s plus device. 不知道iPhone 6s plus设备出了什么问题。 Either issue is with PickerView or something else. 无论是PickerView还是其他问题。 But why the same code works well for all other device and not only for iPhone 6s plus device. 但是,为什么相同的代码对所有其他设备都适用,而不仅对iPhone 6s plus设备适用?

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 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 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;
}

This answer is for future readers, may be it will help someone somehow. 此答案适用于将来的读者,可能会以某种方式帮助某人。

After doing a lot R & D and debugging the code, finally resolved my issue. 经过大量的研发和调试代码,终于解决了我的问题。

Code which I added in my current(check code of above question) code is as below, 我在当前(上述问题的检查代码)代码中添加的代码如下,

NSLocale *locale = [NSLocale localeWithLocaleIdentifier:@"en_US_POSIX"];
NSDateFormatter *format = [[NSDateFormatter alloc] init];
[format setLocale:locale];
[format setTimeZone:[NSTimeZone systemTimeZone]];

Before adding above lines of code(added these lines where ever I have used NSDateFormatter ) it was working fine in all other devices except iPhone 6s plus. 在添加以上代码行(在我使用过NSDateFormatter地方添加这些行)之前,它在除iPhone 6s plus之外的所有其他设备上NSDateFormatter正常工作。 But by setting NSLocale and TimeZone to NSDateFormatter it is working perfectly fine in ALL devices. 但是通过将NSLocaleTimeZone设置为NSDateFormatter它在所有设备中都可以正常工作。

Thanks! 谢谢!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM