Swift 3 how to make background image fill the entire UIView?

Viewed 10309

I've created one UIView and change the background image to it by using the following code

slot1_view?.backgroundColor = UIColor(patternImage: UIImage(named: "drum_cut.png")!)

It's working fine on iphone but on ipad the background image become repeated.

How can I change its contentmode to something the fill the entire UIView?

Please help!!!!

4 Answers

If you're using Swift5, try this on a button click event. you might need to do some layout margin for the UIView

        myView?.contentMode = UIView.ContentMode.scaleToFill
        myView.layer.contents = UIImage(named:"bg.png")?.cgImage


         self.view.bringSubviewToFront(myView)

Swift 5 This work fine for view background image

let backgroundImage = UIImageView(frame: UIScreen.main.bounds)
 backgroundImage.image = AppImageAssets.appAssets.appBackgroundImage()
 backgroundImage.contentMode =  UIView.ContentMode.scaleAspectFill
 self.view.insertSubview(backgroundImage, at: 0)
Related