Repeating background image in native iPhone app

天大地大妈咪最大 提交于 2019-11-27 08:59:40

问题


Short of putting a UIWebView as the back-most layer in my nib file, how can I add a repeating background image to an iPhone app (like the corduroy look in the background of a grouped UITableView)?

Do I need to create an image that's the size of the iPhone's screen and manually repeat it using copy and paste?


回答1:


Apparently a UIColor is not necessarily a single color, but can be a pattern as well. Confusingly, this is not supported in Interface Builder.

Instead you set the backgroundColor of the view (say, in -viewDidLoad) with the convenience method +colorWithPatternImage: and pass it a UI Image. For Instance:

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithPatternImage: [UIImage imageNamed:@"gingham.png"]]; 
}

Of course, don't forget to add the image file to your application bundle.

There are also some built-in background pattern "colors":

  • groupTableViewBackgroundColor
  • viewFlipsideBackgroundColor

Because the are used globally across all iPhone apps, you incur the double-edged sword of an OS update updating the look and feel of your application (giving it a fresh new look that may or may not work right).

For instance:

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor]; 
}



回答2:


You should have a look at the QuartzDemo iPhone example code from Apple, specifically QuartzImageDrawing.m. Should use the following method call.

CGContextDrawTiledImage



回答3:


You can even have an animated tiled background images that move. :D

Apps Amuck has a simple tutorial that show you how to do this on their site.

31 Days of iPhone Apps - Day 16: World Tour




回答4:


You should use colorWithPatternImage



来源:https://stackoverflow.com/questions/224503/repeating-background-image-in-native-iphone-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!