iPhone设置视图背景图片的方法

来源:互联网 发布:淘宝全屏导航栏颜色 编辑:程序博客网 时间:2024/06/05 14:18

方法一,使用一个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. [self addSubview:background];
  7. NSLog(@"Added background subview %@"background);
  8. [self sendSubviewToBack:background];
  9. }

 

方法二,CookBook中提到的方法

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


方法三,lvyile网友用的一个小技巧,uiView是UIView的实例,而不是UIImageView

Objective-c代码
  1. uiView.backgroundColor [UIColor colorWithPatternImage:[UIImage imageNamed:@"Default.png"]];
0 0