【问题标题】:Use two different nib files for iPhone 4 and iPhone 5为 iPhone 4 和 iPhone 5 使用两个不同的 nib 文件
【发布时间】:2013-10-04 04:03:43
【问题描述】:

有没有办法检测用户正在使用什么设备,然后使用该信息,使应用程序使用特定的nib 文件?

现在,我的代码是

- (BOOL)application: (UIApplication *)application didFinishLaunchingWithOptions: (NSDictionary *)launchOptions{
  if(isiPhone5){
    self.RootViewController = @"RootViewController_iPhone5";
  }
  else{
    self.RootViewController = @"RootViewController_iPhone4";
  }
}

我用于查找设备的其他代码有效并告诉我用户正在使用什么设备,但实际上并没有将 .xib 文件从 RootViewController_iPhone4.xib 更改为 RootViewController_iPhone5.xib。有没有办法做到这一点?

我不想使用自动布局,因为我希望根据设备进行其他自定义操作,例如,如果正在使用的设备是 iPhone 4 和 iPhone 5,则视图控制器中的按钮名称不同

【问题讨论】:

  • 使用 AutoSizing 而不是创建两个差异 XIB。
  • 这是一个简单的例子。 techotopia.com/index.php/…
  • [[UIDevice currentDevice] platformString] 将为您提供 iphone 设备版本名称...
  • @PuneethKamath 太棒了!现在如果 iphone 是 iphone 4/5,它会返回什么?

标签: objective-c xib nib


【解决方案1】:

试试这个方法..

if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)
{
    CGRect screenBounds = [[UIScreen mainScreen] bounds];
    if (screenBounds.size.height ==568)
    {
          self.viewController = [[ViewController alloc] initWithNibName:@"ViewController_iPhone5" bundle:nil];
    }
    else
    {
          self.viewController = [[ViewController alloc] initWithNibName:@"ViewController_iPhone4" bundle:nil];
    }

更新答案

检查这个... Black Screen for screen detection

【讨论】:

  • 查看我的更新答案,并且必须确保将启动画面用于屏幕检测。
  • 我使用的是最新版本。此外,它可以检测用户正在使用什么设备,只是当我尝试该代码时它不会将 .xib 文件从 RootViewController 更改为 RootViewController_iPhone5。你有什么其他想法,或者我必须做的其他事情,比如为 RootViewController_iPhone5 制作新的类文件?
  • 愚蠢的问题——这段代码会去哪里(哪个文件)?
【解决方案2】:
#define IS_IPHONE_5 ( fabs( ( double )[ [ UIScreen mainScreen ] bounds ].size.height - ( double )568 ) < DBL_EPSILON )



- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
     if (IS_IPHONE_5)
    {
        self = [super initWithNibName:@"YourViewController~iphone5" bundle:nil];
    }
    else
    {
        self = [super initWithNibName:@"YourViewController~iphone" bundle:nil];
    }
    return self;
}

试试这样的

【讨论】:

  • 我得到 “UIResponder”没有可见的@interface 声明选择器“initWithNibName:bundle:”
  • 你在创建视图控制器吗?还是风景?
【解决方案3】:

您可以通过以下方式实现此目的

在 App 委托中创建一个宏,以便在整个项目中使用它。它将基于设备的屏幕高度。

#define IS_IPHONE_5 ( fabs( ( double )[ [ UIScreen mainScreen ] bounds ].size.height - ( double )568 ) < DBL_EPSILON )

现在您可以测试 iphone 4 和 iphone 5 的情况

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone){


    if(IS_IPHONE_5){


        self.window.RootViewController = [[RootViewController alloc] initWithNibName:@"RootViewController_iPhone5" bundle:nil];

    }
    else
    {
        self.window.RootViewController = [[RootViewController alloc] initWithNibName:@"RootViewController" bundle:nil];
    }
    }
  }

【讨论】:

    【解决方案4】:
    if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
    {
        CGSize result = [[UIScreen mainScreen] bounds].size;
        if(result.height == 480)
        {
            // iPhone 4 Classic
    
            //first always target 4 for better performance & practice.
        }
        if(result.height == 568)
        {
            // iPhone 5
        }
    }else {
    //it's ipad
    }
    

    【讨论】:

      猜你喜欢
      • 2012-11-10
      • 2013-05-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-03
      相关资源
      最近更新 更多