iPhone 6s plus 设备中的 UIPickerView UI 和功能问题
Posted
技术标签:
【中文标题】iPhone 6s plus 设备中的 UIPickerView UI 和功能问题【英文标题】:UIPickerView UI and Functional issue in iPhone 6s plus device 【发布时间】:2016-06-07 06:48:03 【问题描述】:我使用 UIPickerView
创建了自定义 TimePicker,取了 3 个 UIPickerView
。
通过选择时间,我正在使用该时间来设置本地通知。因为我的应用是基于报警功能的。
UI 部分和功能部分在 iPhone 5、5s、6、6 Plus 中运行良好,但在 iPhone 6s plus 设备中遇到问题。 不明白怎么回事!相同的代码在除 iPhone 6s plus 之外的所有其他设备上运行良好。
下面是 iPhone 6 的截图,
如您所见,ScreenShot 时间和 PickerView 时间是相同的。它是功能部件。我已经编写了诸如捕获当前时间之类的代码,并根据我设置了 PickerView 的选定索引。完美运行!我也将选定的索引字体设置得更大。这在 iPhone 6 设备上看起来也很完美。
以下截图是 iPhone 6s Plus,
如您所见,ScreenShot 时间和PickerView
时间不一样。所以功能部分在 iPhone 6s plus 设备中不起作用。同样在 UI 部分,第二个PickerView
中选定行的字体变大,但在第一个中没有。
当我通过从时间选择器中选择时间来设置本地通知时。它在除 iPhone 6s plus 以外的所有其他设备上也能正常工作(通知会在完美时间触发)。在 iPhone 6s 中,无论何时我从UIPickerView
中选择,通知都会立即触发。
不知道 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;
【问题讨论】:
自动布局似乎有问题,如果您正在使用它。是否缺少约束或额外约束?您可以设置文本字段的背景颜色以查看字体是否更小,或整个视图。 不,我没有使用自动布局。我使用自动调整大小来管理它。而且不仅是 UI 问题。 iPhone 6s plus 也面临功能问题。 iPhone 6s plus 是设备还是模拟器?并运行哪个 ios ?其他运行良好的设备或模拟器正在运行 ios? 你能展示你的故事板吗? 无论如何看看你的代码也会很有趣。你能把更新这些元素的地方贴出来吗? 【参考方案1】:这个答案是为未来的读者准备的,可能会对某人有所帮助。
经过大量的研发和调试代码,终于解决了我的问题。
我在当前(上述问题的检查代码)代码中添加的代码如下,
NSLocale *locale = [NSLocale localeWithLocaleIdentifier:@"en_US_POSIX"];
NSDateFormatter *format = [[NSDateFormatter alloc] init];
[format setLocale:locale];
[format setTimeZone:[NSTimeZone systemTimeZone]];
在添加上述代码行之前(在我使用过NSDateFormatter
的地方添加了这些行),它在除 iPhone 6s plus 之外的所有其他设备上运行良好。但是通过将NSLocale
和TimeZone
设置为NSDateFormatter
,它在所有 设备中都可以正常工作。
谢谢!
【讨论】:
以上是关于iPhone 6s plus 设备中的 UIPickerView UI 和功能问题的主要内容,如果未能解决你的问题,请参考以下文章
Alamofire 上传仅在 iphone 6s plus 上崩溃
c_cpp Taptic API for iPhone 7,7 Plus(公共API)。还适用于iPhone 6s,6s Plus(私有API)。