iphone中背景图的设置方法

本文详细介绍了在 iOS 中如何通过多种方法设置背景图,包括使用 UIImageView 实例作为子视图、自定义视图层级管理、利用 UIView 的 backgroundColor 属性以及直接在 nib 中设置等方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

iphone中背景图的设置
 
方法一,使用一个UIImageView实例做子视图,并且放最后面

- (void)setBackgroundImage {  
    NSLog(@"setting bg image");  
    UIImageView *customBackground = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"background.jpg"]];  
    self.background = customBackground;  
    [customBackground release];  
      
    [self addSubview:background];  
    NSLog(@"Added background subview %@", background);  
    [self sendSubviewToBack:background];  

解读: UIImageView除了可以用于背景图的设置之外,其它任何区域的设置,都可以用UIImageView来实现。


方法二,Cook Book中提到的方法
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {  
      
    UIImageView *contentView = [[UIImageView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];  
    [contentView setImage:[UIImage imageNamed:@"Default.png"]];  
    [contentView setUserInteractionEnabled:YES];  
    self.view = contentView;  
    [contentView release];  

解读:正如注解中所说,当不希望用nib的时候,这个loadView就派上和场了。默认情况下,loadView是被注释掉的。

方法三,lvyile网友用的一个小技巧,参考代码如下
uiView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"Default.png"]]; 
 
解读:uiView是UIView的实例,而不是UIImageView

方法四,直接在nib中设置,这是最常用的一种方法,这里就不多说了

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

liranke

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值