Last active
April 12, 2021 20:26
-
-
Save mukhortov/aed48b02aa204668a81609b88ab9d553 to your computer and use it in GitHub Desktop.
Add background image to UIView
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks!