【问题标题】:iOS 6 loadNibNamed loads nib of iphone5 in 3.5 inch phonesiOS 6 loadNibNamed 在 3.5 英寸手机中加载 iphone5 的 nib
【发布时间】:2014-06-28 12:25:14
【问题描述】:

我有一个 UIView 子类 (PopupView) 和两个 xbis:PopupView.xib 和 PopupView~iphone5.xib。

当我调用 xib 时:

    if ( screenHeight >= 568 ) {
        subviewArray = [[NSBundle mainBundle] loadNibNamed:@"PopupView~iphone5" owner:self options:nil];
    } else {
        subviewArray = [[NSBundle mainBundle] loadNibNamed:@"PopupView" owner:self options:nil];
    }

在 iOS 7 中,使用 3.5 英寸手机进行测试时,它会加载正确的 PopupView.xib,而在 4 英寸手机上,它会加载 PopupView~iphone5.xib。 但是在使用 iOS 6 和 3.5 英寸手机进行测试时,虽然代码从“else”语句传递并且理论上它加载了 PopupView.xib ,但它实际上返回了 iphone 5 xib (显示的视图是为 iphone5 设计的)和加载视图的高度是 548,而不是 480。

有什么建议吗?

【问题讨论】:

  • 568 >= 548 返回与 568 >= 568 相同的值。
  • 与其猜测和理论推测遵循哪个执行路径,您可以使用日志语句或断点吗?另外,我只能猜测您计算 screenHeight 的方式可能不适用于 iOS 6,因此请包含该代码。
  • @Ramdy 你是对的,这是我的错字,但 568(iphone 5 屏幕高度)对于 548 和 568 值都是 >=。所以这并不能解决问题。
  • @nhgrif 我不是在猜测。我已经用调试器检查了它,是的,代码加载了 else 语句中的部分。这部分叫做:subviewArray = [[NSBundle mainBundle] loadNibNamed:@"PopupView" owner:self options:nil];但仍会加载 iphone5 xib
  • 你如何确定它正在加载不正确的 xib?

标签: ios iphone loadnibnamed


【解决方案1】:

已修复。我所需要的只是清理完整版本,并从模拟器中删除应用程序并重建。希望这个案例可以帮助其他面临类似问题并且不记得清理他的项目的人。

【讨论】:

    【解决方案2】:

    尝试使用以下代码。

    -(id) init{
        UIScreen* mainscr = [UIScreen mainScreen];
        CGSize screenSize = mainscr.currentMode.size;
        CGFloat screenHeight = screenSize.height;
        if (screenHeight == 1136) {
            self = [super initWithNibName:@"PopupView~iphone5" bundle:[NSBundle mainBundle]];
        }
        else
        {
            self = [super initWithNibName:@"PopupView" bundle:[NSBundle mainBundle]];
        }
    }
    

    它对我有用。

    【讨论】:

    • 正如我所说...代码位于 if-else 语句的正确位置。 loadNibNamed 工作错误。另外,我不是在加载 UIViewController,而是在加载 UIView
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多