Forked from mukhortov/UIView+backgroundImage.swift
Created
September 30, 2018 14:50
-
-
Save johnteee/84c8004d340f3d019de4ee1b85dc8b26 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