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 use this...
The
colorWithPattern:
method is really for generating patterns from images. Thus, the customization you require is most likely not possible, nor is it meant to be.Indeed you need to use a
UIImageView
to center and scale an image. The fact that you have aUIScrollView
does not prevent this:Make
self.view
a generic view, then add both theUIImageView
and theUIScrollView
as subviews. Make sure all is wired up correctly in Interface Builder, and make the background color of the scroll view transparent.This is IMHO the simplest and most flexible design for future changes.
For Swift 3.0 use the following code:
You can use UIGraphicsBeginImageContext method to set the size of image same that of view.
Syntax : void UIGraphicsBeginImageContext(CGSize size);