【问题标题】:What piece am I missing to set an iOS background?我缺少什么来设置 iOS 背景?
【发布时间】:2013-10-04 01:17:52
【问题描述】:

我有:

[一系列 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];

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

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

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

【问题讨论】:

  • 这一切似乎都是正确的。我们需要查看更多代码。您在哪种方法中添加视图?您要向哪个视图添加containerView
  • 你能放更多代码吗?我假设您正在将 containerView 添加到 self.view。非视网膜图像也是 Default-Background-Labdscape.png(带有 .png 扩展名)。
  • @GuyKogus,谢谢;我在代码中添加了更多上下文。
  • @JonathanHayward 在代码中添加图像扩展名并尝试。在这段代码中 backgroundImage = [UIImage imageNamed:@"Background-Default-Portrait"];
  • 为什么要使用设备方向通知?如果您使用的是UIViewController 类,则使用给定的界面方向更改方法。另外,使用CGFloat 而不是int 作为宽度和高度。与您的数据类型保持一致! :)

标签: iphone ios ipad uiimageview background-image


【解决方案1】:

每次 renderScreen 触发它都会在当前控制器的主视图中添加两个子视图:

[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];

这样视图就被重绘了。

【讨论】:

  • 谢谢;我看到了不同的行为。它从黑色开始,然后变为白色。 (无论是否使用 [self.containerView setNeedsDisplay]; 或 [self.view setNeedsDisplay]; 都是如此)
  • 横向图片在哪里设置?如果屏幕高度不是以下之一: (height == 2048 || height == 2008 || height == 1024 || height == 1004 || height == 984) 那么 backgroundImage 仍然是 nil
  • 代码是我遇到的一个问题的适应。在竖屏模式下,高度为2048,宽度为1536......而在横屏模式下,高度仍然是2048,宽度仍然是1536。“if(UIInterfaceOrientationIsPortrait([UIApplication sharedApplication].statusBarOrientation))”是一个测试,以确定高度是否真的是高度,或者高度真的是宽度。
猜你喜欢
  • 1970-01-01
  • 2019-05-30
  • 2015-10-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-13
  • 2021-09-19
相关资源
最近更新 更多