I have an UIView
and I set a background image in this way:
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"sfond-appz.png"]];
My problem is that back-image is not centered inside the view, but it's replayed some times to fill all the view. Is there a way to center image inside uiview and scretch to have screen size?
Note: I can't use UIImageView
for background cause I have a scrollview
.
For Swift 2.1 use this...
Correct way to do in Swift 4, If your frame as screen size is correct then put anywhere otherwise, important to write this in
viewDidLayoutSubviews
because we can get actual frame inviewDidLayoutSubviews
Swift 4 Solution :
You need to process the image beforehand, to make a centered and stretched image. Try this:
The marked answer is fine, but it makes the image stretched. In my case I had a small tile image that I wanted repeat not stretch. And the following code was the best way for me to solve the black background issue:
Repeat:
Stretched