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

UIPickerView UI and Functional issue in iPhone 6s plus device

我使用 UIPickerView 创建了自定义 TimePicker,已使用 3 UIPickerView

通过选择时间,我正在使用那个时间来设置本地通知。因为我的应用是基于报警功能的。

UI 部分和功能部分在 iPhone 5,5s,6,6 Plus 中运行良好,但在 iPhone 6s plus 设备中面临问题。 不知道出了什么问题!相同的代码在除 iPhone 6s plus.

之外的所有其他设备中都能正常工作。

下面是iPhone6,

的截图

如您所见,ScreenShot 时间和 PickerView 时间相同。它是功能部分。我编写了类似代码,捕捉当前时间,并根据我设置了 PickerView 的 selected 索引。工作完美!我还设置了更大的 selected 索引字体。这在 iPhone 6 设备中看起来也很完美。

下面是iPhone6s Plus的截图,

如您所见,截屏时间和PickerView时间不一样。所以功能部分在 iPhone 6s plus 设备中不工作。同样在 UI 部分,第二行 selected 行的字体变大 PickerView 而不是第一行。

还有当我通过从时间选择器中选择时间来设置本地通知时。它在 iPhone 6s plus 以外的所有其他设备中也能很好地工作(通知在完美的时间触发)。在 iPhone 6 秒加上我从 UIPickerView select 的任何时间,通知立即触发。

不知道iPhone 6s plus 设备出了什么问题。这两个问题都与 PickerView 或其他问题有关。但为什么相同的代码适用于所有其他设备,而不仅仅是 iPhone 6s plus 设备。

感谢任何帮助!严重卡在这个问题上。如果有人对此问题有任何解决方案,请提供帮助!提前致谢。

更新:

请查看我的代码...

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

这个答案是为未来的读者准备的,可能会对某些人有所帮助。

经过大量的研发和调试代码,终于解决了我的问题。

我在当前(检查上述问题的代码)代码中添加的代码如下,

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

在添加以上代码行之前(在我曾经使用过的地方添加这些行 NSDateFormatter)它在除 iPhone 6s plus 之外的所有其他设备上工作正常。但是通过将 NSLocaleTimeZone 设置为 NSDateFormatter 它在 ALL 设备中工作得很好。

谢谢!