ios - 为 iPod、iPhone 和 iPad 设置背景图像?

标签 ios objective-c

我正在尝试覆盖不同的背景图像以在我的应用程序上运行。我使用了这个问题的代码; set background images for Iphone 3g, Iphone 4s and Iphone 5

谢天谢地,代码很简单。为了以防万一,我还在 .m 文件中设置了这些。

.h

@property (retain strong) UIImage* Image 

@property (retain, strong) UIImageView.

.m

@interface ViewController ()

@end

@implementation ViewController

//SYNTHESIZE JUST IN CASE

@synthesize backgroundImageView=backgroundImageView;
@synthesize Image=Image;

- (void)viewDidLoad

{

//SETS A UIIMAGEVIEW TO BE SET TO A CUSTOM UIIMAGE (JPG)

UIImageView *backgroundImageView = [[UIImageView alloc] initWithImage:Image];
[backgroundImageView setFrame:[[self view] bounds]];
[[self view] addSubview:backgroundImageView];

//I EXCPECTED THIS NEXT LINE OF CODE TO WORK TO SET A SEPARATE BACKGROUND FOR IPAD...

UIImage *Image = [[UIImage alloc]init];
if ([[UIScreen mainScreen] bounds].size.height == 568) {
    Image = [UIImage imageNamed:@"11@2x.jpg"];
}
else

//THIS ELSE STATEMENT IS THE IMAGE THAT ACTUALLY RASTERIZES FOR BOTH IPAD AND IPOD TEST DEVICES

{
    Image = [UIImage imageNamed:@"7@2x.jpg"];
}

self.view.backgroundColor = [UIColor colorWithPatternImage:Image];


[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}

覆盖未按预期正常工作。无论我在 if 语句中设置什么图像,都只会运行 else 语句。

我的问题; if 语句中的尺寸 (568) 对于为 iPad 设备光栅化单独的图像是否正确?如果没有,请告诉我,谢谢。

附加信息:部署目标:6.1 附加信息:我在运行时收到一个错误(即使它仍然运行),类似于我的 nib 文件和 Storyboard之间的差异。不过我没有使用 Nib 。 ?

最佳答案

以下是正确的值:

if ([[UIScreen mainScreen] bounds].size.height == 480) {
    // iPhone, iPod Touch
}

if ([[UIScreen mainScreen] bounds].size.height == 568) {
    // iPhone 5
}

if ([[UIScreen mainScreen] bounds].size.height == 750) {
    // iPhone 6
}

if ([[UIScreen mainScreen] bounds].size.height == 1024) {
    // iPad
}

if ([[UIScreen mainScreen] bounds].size.height == 1242) {
    // iPhone 6 Plus
}
  1. iPhone [UIScreen mainScreen] bounds] {{0, 0}, {320, 480}}
  2. iPhone 5 [UIScreen mainScreen] bounds] {{0, 0}, {320, 568}}
  3. iPhone 6 [UIScreen mainScreen] bounds] {{0, 0}, {750, 1334}}
  4. iPad [UIScreen mainScreen] bounds] {{0, 0}, {768, 1024}}
  5. iPhone 6 Plus [UIScreen mainScreen] bounds] {{0, 0}, {1242, 2208}}

编辑:添加了 iPhone 6 和 6 Plus 以根据 Jules '评论维护此列表,但不建议使用此方法区分手机甚至选择正确的图像。


正确的方法

使用 Assets 目录。

关于ios - 为 iPod、iPhone 和 iPad 设置背景图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24602007/

相关文章:

ios - 全局数组未附加

iphone - 自定义委托(delegate)和数据源的问题

objective-c - 您如何知道用户何时选择通过AirPlay播放视频?

ios - 比较两个 NSNumber 对象

ios - 使用 ARC 的堆增长

iphone - 在 Storyboard 中编辑自定义 UIView 子类

objective-c - 无法将 Swift 代码导入 Obj-C

ios - avplayer 在模拟器上而不是在设备上播放视频?

objective-c - 如何在 iOS 的 Objective-C 中创建和保存 .rtf、.doc、.docx

ios - 如何使用印度卢比将 NSInteger 转换为 objective-c 中的相应单词