Skip to content

Instantly share code, notes, and snippets.

@mukhortov
Last active April 12, 2021 20:26
Show Gist options
  • Save mukhortov/aed48b02aa204668a81609b88ab9d553 to your computer and use it in GitHub Desktop.
Save mukhortov/aed48b02aa204668a81609b88ab9d553 to your computer and use it in GitHub Desktop.
Add background image to UIView
import UIKit
extension UIView {
func backgroundImage(named: String) {
let backgroundImage = UIImageView(frame: self.frame)
backgroundImage.image = UIImage(named: named)
backgroundImage.contentMode = .scaleAspectFill
backgroundImage.translatesAutoresizingMaskIntoConstraints = false
backgroundImage.center = self.center
backgroundImage.autoresizingMask = [.flexibleLeftMargin, .flexibleRightMargin, .flexibleTopMargin, .flexibleBottomMargin]
self.insertSubview(backgroundImage, at: 0)
self.sendSubview(toBack: backgroundImage)
}
}
@ListopadovArt
Copy link

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment