【问题标题】:Storyboarding, Interface Builder not loading 568h images故事板,Interface Builder 未加载 568h 图像
【发布时间】:2012-11-01 08:19:21
【问题描述】:

我有一个名为 heart.png 的全屏图像加载到 320x480 的 UIImageView 中。我创建了一个 640x960 版本并将其命名为 heart@2x.png。我还创建了一个 640x1136 的版本并将其命名为 heart-568h@2x.png。

我在情节提要中构建了一个 3.5" 视图,其 UIImageView 为 320x480。我假设使用 4" 屏幕设备会自动加载 heart-568h@2x.png 但似乎并非如此。我必须为 3.5" 和 4" 屏幕创建 2 个故事板吗?

编辑

到目前为止,一切都在 IB 中完成,没有使用任何代码。这是我刚刚设置的示例项目:

【问题讨论】:

标签: ios ios6 autolayout


【解决方案1】:

你是对的。设备不会以区分常规和视网膜 (@2x) 的方式自动加载 568 图像(启动图像除外)。您必须以编程方式检查或使用单独的故事板/NIB。

if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
        CGSize screenSize = [[UIScreen mainScreen] bounds].size;
        if(screenSize.height == 568) {
           // ...
        }
}

【讨论】:

  • 希望不会太离题,但我有很多观点,其中有几个资产是专为 4" 屏幕设计的。设计两个 Storyboard 是否有意义,一个用于 3.5",一个用于4 英寸?
  • 这是我前进的方向......但我不愿意说这是否是最佳实践。
【解决方案2】:

我像这样构建了自己的自定义 ImageView 类:

//  UICustomImageView.h
@interface UICustomImageView : UIImageView
@property (nonatomic) NSString *filename568;
@end

//  UICustomImageView.m
#import "UICustomImageView.h"

@implementation UICustomImageView

- (void)awakeFromNib {
    [super awakeFromNib];

    [self checkFor568];
}

-(void)drawRect:(CGRect)rect {
    [super drawRect:rect];

    [self checkFor568];
}

-(void)checkFor568 {
    if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
        CGSize screenSize = [[UIScreen mainScreen] bounds].size;
        if(screenSize.height == 568) {
            if (self.filename568 != nil)
                self.image = [UIImage imageNamed:self.filename568];
        }
    }
}

@end

像这样在 Interface Builder 中使用它。 (仍将图片命名为 Background-568h@2x.png 进行缩放):

【讨论】:

  • 这是通过 IB 做到这一点的最简单、最轻松的方法!谢谢!
【解决方案3】:

通过堆栈溢出搜索,我了解到图像不会自动加载名称中带有 -568h 的图像。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-01-30
    • 1970-01-01
    • 1970-01-01
    • 2017-05-05
    • 2012-01-21
    • 2012-08-12
    • 1970-01-01
    • 2019-10-31
    相关资源
    最近更新 更多