Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save johnteee/84c8004d340f3d019de4ee1b85dc8b26 to your computer and use it in GitHub Desktop.
Save johnteee/84c8004d340f3d019de4ee1b85dc8b26 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)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment