方法一,使用一个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"]];