Iphone 我在设置iOS背景时遗漏了什么?

Iphone 我在设置iOS背景时遗漏了什么?,iphone,ios,ipad,uiimageview,background-image,Iphone,Ios,Ipad,Uiimageview,Background Image,我有: [一系列if语句将backgroundImage设置为类似于[UIImageName@“Background Default scape”];我是否正确地假设设备将查找名为Background Default的文件-Landscape@2x.png如果是视网膜显示器?] UIImageView *backgroundView = [[UIImageView alloc] initWithImage:backgroundImage]; CGRect containerRect = CGRe

我有:

[一系列if语句将backgroundImage设置为类似于[UIImageName@“Background Default scape”];我是否正确地假设设备将查找名为Background Default的文件-Landscape@2x.png如果是视网膜显示器?]

UIImageView *backgroundView = [[UIImageView alloc] initWithImage:backgroundImage];
CGRect containerRect = CGRectZero;
containerRect.size = [backgroundImage size];
UIView *containerView = [[UIView alloc] initWithFrame:containerRect];
[containerView addSubview:backgroundView];
现在我的应用程序正在启动,加载后会显示一个白色屏幕,而不是指定的背景(没有一个背景是纯白的)

在我开始把东西放在背景上之前,在下面的代码中,我缺少什么来绘制背景呢

谢谢

--编辑--

就代码而言,我有:

- (void) renderScreen
{
    int height = [[UIScreen mainScreen] bounds].size.height;
    int width = [[UIScreen mainScreen] bounds].size.width;

    UIImage *backgroundImage = nil;

    if (height == 2048 || height == 2008 || height == 1024 || height == 1004 || height == 984)
    {
        backgroundImage = [UIImage imageNamed:@"Background-Default-Portrait"];
    }

    // Other such statements cut. 
    UIImageView *backgroundView = [[UIImageView alloc] initWithImage:backgroundImage];
    CGRect containerRect = CGRectZero;
    containerRect.size = [backgroundImage size];
    UIView *containerView = [[UIView alloc] initWithFrame:containerRect];

    [containerView addSubview:backgroundView];
    [self.view addSubview:containerView];
下面是在背景上绘制的语句

加载初始视图并旋转时调用此方法:

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self renderScreen];
    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(renderScreen) name:UIDeviceOrientationDidChangeNotification object:nil];
}
其行为是,正确的初始屏幕加载,然后它会变为白色,之后似乎没有什么有趣的事情发生

--编辑--

在顶部,我有:

int height = [[UIScreen mainScreen] bounds].size.height;
int width = [[UIScreen mainScreen] bounds].size.width;
当我记录高度和宽度时,对于处于横向模式的视网膜设备,我得到的高度为1024,宽度为768。显示的图像是纵向图像的旋转;如果我从侧面转动设备,图像会整齐地填满整个屏幕,但背景会显示一个水平压缩的图像


为了正确获得高度和宽度,我应该做些不同的事情吗?我预计横向设备的高度为768,宽度为1024。

每次renderScreen启动时,它将向当前控制器的主视图添加两个子视图:

[containerView addSubview:backgroundView];
[self.view addSubview:containerView];
每次旋转设备时都会触发

至少将containerView作为属性并替换它

@property (nonatomic, strong) UIView *containerView;
和内部-(无效)渲染屏幕

[self.containerView  removeFromSuperview];
self.containerView = [[UIView alloc] initWithFrame:containerRect];
[self.view addSubview:self.containerView];
您还可以添加:

[self.view setNeedsDisplay];

以便重新绘制视图。

这一切似乎都是正确的。我们需要看到更多的代码。使用哪种方法添加视图?您正在向哪个视图添加
containerView
?是否可以添加更多代码?我猜您正在将containerView添加到self.view。另外,非视网膜图像是Default-Background-Labdscape.png(扩展名为.png)。@GuyKogus,谢谢;我在代码中添加了更多上下文。@JonathanHayward在代码中添加图像的扩展,然后重试。在此代码中,backgroundImage=[UIImage ImageName:@“背景默认肖像”];为什么要使用设备定向通知?如果使用的是
UIViewController
类,则使用给定的接口方向更改方法。另外,使用
CGFloat
not
int
表示宽度和高度。与您的数据类型保持一致!:)非常感谢。我看到了不同的行为。它从黑色开始,然后变为白色。(无论是否使用[self.containerView setNeedsDisplay];或[self.view setNeedsDisplay];都是如此)在何处设置横向图像?如果屏幕高度不是以下各项之一:(高度==2048 | |高度==2008 | |高度==1024 | |高度==1004 | |高度==984),则背景图像仍然为零。代码是我遇到的一个问题的解决方案。在纵向模式下,高度为2048,宽度为1536。。。在横向模式下,高度仍然是2048,宽度仍然是1536。“如果(UIInterfaceOrientationSportRait([UIApplication sharedApplication].statusBarOrientation))”是一个测试,用于确定高度是真高度还是真宽度。