0

I have universal app. In that app I need the background image to be centered or stretched. That way when I rotate the device or switch it the image displays correctly.

Here is my code in viewDidLoad:

self.view.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"tux.png"]]

Can anyone tell me how to accomplish this?

Ky -
  • 30,724
  • 51
  • 192
  • 308
Alippo
  • 13
  • 2
  • 5

1 Answers1

0

I think you can't easily make it centered with backgroundColor. You may:

  • Stretch it before setting the image as backgroundColor, see How to fill background image of an UIView.

  • Create a customized UIView subclass and draw the background image yourself by overriding drawRect: method;

  • Or add a UIImageView to you view as the background. See "UIView Class Reference: Alternatives to Subclassing":

    Image-based backgrounds - For views that display relatively static content, consider using a UIImageView object with gesture recognizers instead of subclassing and drawing the image yourself. Alternatively, you can also use a generic UIView object and assign your image as the content of the view’s CALayer object.

Community
  • 1
  • 1
Hailei
  • 42,163
  • 6
  • 44
  • 69
  • didn't work with stretch when i rotate iPhone, if u could help me with centered background i will appreciate. – Alippo Apr 28 '12 at 17:58