I have a method for repeating or tiling an image across a view using UIColor
patternImage
:
view.backgroundColor = UIColor(patternImage: UIImage(named:imageName))
The default behaviour of UIColor
patternImage
is to start the pattern from the top left, the result shown in Image 1.
Question:
How can I get UIColor
patternImage
to start always from the very center of a view, patterning outwards to give the result shown in Image 2?
This works. It is a generalised method that applies more broadly, adapting dynamically to both pattern images and views of different heights and widths. It gives the desired result in the question and is tested on different iOS simulators.
view.backgroundColor = UIColor(patternImage: UIImage(named: "imageName")!)
view.bounds.origin.x = (UIImage(named: "imageName")!.size.width/2) - (view.bounds.size.width/2)
view.bounds.origin.y = (UIImage(named: "imageName")!.size.height/2) - (view.bounds.size.height/2)