【问题标题】:UIViewController with background image带有背景图像的 UIViewController
【发布时间】:2011-02-28 18:45:12
【问题描述】:

如何将资源中的图像作为背景图像插入我的 UIViewController? 提前致谢!

【问题讨论】:

    标签: iphone ipad uiviewcontroller background-image


    【解决方案1】:

    UIViewControllers 没有背景图片。只有视图本身具有视觉属性。

    UIView 没有背景图片属性。要显示背景图像,您通常只需将显示图像的 UIImageView 放在视图层次结构中,以便它在视觉上显示在所有其他视图的后面。这可以通过编程方式或在 Interface Builder 中完成。

    【讨论】:

    • 谢谢,我查看了很多关于背景图片的帖子,并尝试了各种不起作用的代码,似乎使用 Storyboards 是最好的方法。谢谢。
    【解决方案2】:

    添加UIViewController的视图,有点像这样:

    - (void) viewDidLoad 
    {
       UIImage *background = [UIImage imageNamed: @"background.png"];    
       UIImageView *imageView = [[UIImageView alloc] initWithImage: background]; 
    
       [self.view addSubview: imageView]; 
    
       [imageView release];
    
       [super viewDidLoad];
    }
    

    【讨论】:

    • 我在顶部看到大约 10 像素的边距。有什么我想念的吗?
    • @vishalashah 尝试将图像视图的框架设置为视图控制器视图的框架,然后再将其添加为子视图。
    • 对于背景视图,您可能想使用[self.view insertSubview: imageView atIndex:0]; 否则它将在您的其他 xib 内容之上。
    • 不需要添加图像视图来这样做,它会使视图控制器加载缓慢。简单地说,在情节提要中将视图的背景更改为任何颜色,然后在 viewDidLoad 添加 [[self view] setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"bg.png"]]]; 它会变得更好,即使滚动也会看起来很棒。
    猜你喜欢
    • 1970-01-01
    • 2015-06-20
    • 1970-01-01
    • 2011-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多