As a first toe-dip in coding, I wanted to create a simple app with a couple of buttons, labels, a nice background, etc. Nothing too fancy, or even functional, just a layout experiment.
Turns out the “nice background” is more of a PITA than it should be. Things I’ve (so far) found I/you can’t (easily) do:
- Interface Builder doesn’t allow you to select an image as the background of a view — just a flat color, with transparency if you’ve got something interesting underneath it
- You can drag an Image View to fill the available space, but you can only define a loaded image file’s position within the Image View, or stretch it to fit — you can’t set it to tile (not in IB, anyway …)
- Searching for “tiled image” or “tiled background” in the Developer Reference Library yields cursory references to using
CGContextDrawTiledImage, or “use UIKit to create a UIColor object from a UIImage object using the method colorWithPatternImage“. (Apparently a UIColor can be a PatternImage.) But the documentation isn’t clear onhow/where you would use this instruction (and I’m too much of a newbie to intuit the answer).
But it’s actually just a single line of code, added to the -ViewDidLoad callback:
-(void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor colorWithPatternImage: [UIImage imageNamed:@"imagefilename.png"]];
}
(Thanks to Frank Schmitt at Stack Overflow.)
So the solution is easy to implement. But, since it’s in code, your nice background doesn’t show up in Interface Builder — you only see it at runtime. This makes it harder to design interface elements to contrast with the background image, and if you’re trying to align things to match the background, well, get used to clicking Build and Go.