iphone - 设置 iOS 背景时我缺少哪一部分?

标签 iphone ios ipad uiimageview background-image

我有:

[一系列if语句将backgroundImage设置为类似[UIImageNamed @"Background-Default-Landscape"];如果设备是 Retina 显示屏,我是否可以假设设备将查找名为 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];

下面是在背景上绘制的语句。

当初始 View 加载和旋转时调用此方法:

- (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;

当我 NSLog 高度和宽度时,对于横向模式下的视网膜设备,我得到的高度为 1024,宽度为 768。显示的图像是纵向图像的旋转;如果我将设备转向一侧,图像会整齐地填充整个屏幕,但背景会显示水平挤压的图像。

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

最佳答案

每次 renderScreen 触发时,它都会向当前 Controller 的主视图添加两个以上的 subview :

[containerView addSubview:backgroundView];
[self.view addSubview:containerView];

每次旋转设备时都会触发此事件。

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

@property (nonatomic, strong) UIView *containerView;

和内部-(void)renderScreen

[self.containerView  removeFromSuperview];
self.containerView = [[UIView alloc] initWithFrame:containerRect];
[self.view addSubview:self.containerView];

您还可以添加:

[self.view setNeedsDisplay];

以便重绘 View 。

关于iphone - 设置 iOS 背景时我缺少哪一部分?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19055895/

相关文章:

ios - 从用户默认值加载数据时,我得到一个空的 CLLocationCoordinates 数组

android - 如何更改应用程序的内容

javascript - 在 ipad 上选择时,jQuery Chosen 选择选项会触发具有较低 z 索引的链接

ios - Swift 中的垂直标签栏

ios - UIAlertController/ActionSheet 在 iPad 上崩溃,如何传递发件人?

iphone - XCode 4.6.3 未检测到具有 7.0.2 的 iphone 设备

iphone - Leaks (instrument) 报告自动释放对象中的泄漏

iphone - iPhone 中的用户凭证持久性

iphone - Interface Builder 中的占位符对象

ios - 如何将 Youtube 帐户链接到 iOS 中的嵌入式 YTPlayerView 播放器?