If you ever need to add a image as background for an UIView then do it like this (Make sure the image is in your app’s resource bundle):-
- (void)viewDidLoad { self.view.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"MyBackground.png"]]; }
Thanks for tip. The able code will fail. Here is the correct code. UIView *contentView = [[UIView alloc] initWithFrame: [[UIScreen mainScreen] applicationFrame]]; //contentView.backgroundColor = [UIColor redColor]; //contentView.backgroundColor = [UIColor viewFlipsideBackgroundColor]; //contentView.backgroundColor = [UIColor groupTableViewBackgroundColor]; //contentView.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"someimage.png"]]; self.view = contentView;
[contentView release];
Tnanks
Both the code are same. In first one you are allocating color to background and and assigning it an image.
In second one again the same thing is happening. The only difference is some extra code. This will increase the allocations of objects when viewed in instruments. there is no need to to first have a UIview as a separate object then assign it to the main view.
Simply extra piece of code.
Thanks.
You have a memory leak there. The correct code is:
UIColor *bkgColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"MyBackground.png"]]; self.view.backgroundColor = bkgColor; [bkgColor release];
How about:
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"background.png"]];
Name (required)
Mail (will not be published) (required)
Website
Please note: JavaScript is required to post comments. Spam protection by WP Captcha-Free