【问题标题】:iPhone build which supports version 4.0 to 5.0 using Xcode 4.2使用 Xcode 4.2 支持 4.0 到 5.0 版本的 iPhone 构建
【发布时间】:2023-03-24 18:28:01
【问题描述】:

您好,我正在使用 xcode 4.2 开发 iOS SDK5,我想准备一个应该与 iOS 4.0 到 5.0 或至少 4.3 和 5.0 兼容的构建

我已将基本 sdk 设置为 5.0 并将部署目标设置为 4.0,但是当我在 iPhone 4 模拟器中运行应用程序时,我的应用程序基本上由于下面编写的代码而崩溃

[testNavigationBar setBackgroundImage:[UIImage imageNamed:@"facebook-navbar.png"] forBarMetrics:UIBarMetricsDefault];

但是当我将 iPhone 模拟器从 4 切换到 5 时,上面的代码运行良好,我也尝试过 setBackgroundColor 但它不起作用,所以请帮我解决这个问题。

【问题讨论】:

    标签: objective-c xcode uinavigationbar xcode4.2 ios5


    【解决方案1】:

    问题是该方法是在iOS5中添加的,所以在iOS4中不存在。您需要进行运行时检查以查看您是否在 iOS5 中,如下所示:

    if ([testNavigationBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics)]) {
        [testNavigationBar setBackgroundImage:[UIImage imageNamed:@"facebook-navbar.png"] forBarMetrics:UIBarMetricsDefault];
    }
    

    现在的问题是您在 iOS4 中无法获得背景,因此您必须找到解决方法。

    【讨论】:

      【解决方案2】:

      这是因为 -setBackgroundImage: 是在 iOS 5 中引入的,Apple 通常不会(通常)将新 API 向后移植到旧平台。无论如何,你有两个选择:

      if ([testNavigationBar respondsToSelector:@selector(setBackgroundImage:)]) {
        // do what you're currently trying
      }
      else {
        //find a compatible way to do it
      }
      

      或:

      //find a compatible way to do it
      

      【讨论】:

        【解决方案3】:

        对于 iOS 4.x 版,您始终可以覆盖 UINavigationBar drawRect。这是关于这个主题的 Stack Overflow 上的帖子:

        Background image for navigation view

        【讨论】:

          猜你喜欢
          • 2019-08-17
          • 2019-10-19
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多