我认为最好为 3.5 和 4 英寸显示器提供两个单独的 nib 文件。是的,我知道这会增加您的工作量,但是是的,代码将来会更易于管理。您只需要提供单独的笔尖。
您可以使用以下方法每次为所需尺寸动态加载笔尖。
-(void)pushViewController:(UIViewController*)viewController withNib:(NSString*)nibName
{
if(nibName){
nibName=[self getNibNameForNib:nibName];
viewController=[viewController initWithNibName:nibName bundle:nil];
[self.navigationController pushViewController:viewController animated:YES];
}
}
-(NSString*)getNibNameForNib:(NSString *)nibName
{
NSString *newNibName=nil;
if([self isIphone5Retina4InchDisplay]){
newNibName=[nibName stringByAppendingString:@"-568h"];
}
if(newNibName && [[NSBundle mainBundle] pathForResource:newNibName ofType:@"nib"] != nil)
{
//if iphone 5 and nib is also present for that resolution.
nibName=newNibName;
}
return nibName;
}
/**
Method to get if device is 4 inch iphone 5 retina device or not.
*/
-(BOOL)isIphone5Retina4InchDisplay
{
CGRect screenBounds = [[UIScreen mainScreen] bounds];
if (screenBounds.size.height == 568) {
// code for 4-inch screen
return YES;
} else {
// code for 3.5-inch screen
return NO;
}
}
只需将 RETINA 4 英寸笔尖名称命名为 LoginViewController-568h.xib 即添加 -568h 后缀。每次使用这个 pushViewController 方法来推送一个新的视图控制器。您可以在基本视图控制器中编写此方法。