iOS 设置背景图片

来源:互联网 发布:吉他中国淘宝 编辑:程序博客网 时间:2024/05/21 10:56
方法一,使用一个UIImageView实例做子视图,并且放最后面

Objective-c代码  收藏代码
  1. - (void)setBackgroundImage {    
  2.     NSLog(@"setting bg image");    
  3.     UIImageView *customBackground = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"background.jpg"]];    
  4.     self.background = customBackground;    
  5.     [customBackground release];    
  6.         
  7.     [self addSubview:background];    
  8.     NSLog(@"Added background subview %@", background);    
  9.     [self sendSubviewToBack:background];    
  10. }    
 


方法二,Cook Book中提到的方法

Objective-c代码代码  收藏代码
  1. - (void)loadView {    
  2.         
  3.     UIImageView *contentView = [[UIImageView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];    
  4.     [contentView setImage:[UIImage imageNamed:@"Default.png"]];    
  5.     [contentView setUserInteractionEnabled:YES];    
  6.     self.view = contentView;    
  7.     [contentView release];    
  8. }    
 

方法三,lvyile网友用的一个小技巧,uiView是UIView的实例,而不是UIImageView
Objective-c代码  收藏代码
  1. uiView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"Default.png"]];   
原味地址:http://gekie.iteye.com/blog/1088096
原创粉丝点击