Created
June 15, 2016 17:50
-
-
Save kusalshrestha/de434e63698ebe9df6de585eb8ec727b to your computer and use it in GitHub Desktop.
Making transparent background (Esp. for UItextField)
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
extension UIView { | |
func makeTransparentView(withOpacity alphaValue: CGFloat, color: UIColor) { | |
let backgroundView = UIView() | |
self.superview?.insertSubview(backgroundView, belowSubview: self) | |
self.backgroundColor = UIColor.clearColor() | |
backgroundView.alpha = alphaValue | |
backgroundView.backgroundColor = color | |
backgroundView.layer.cornerRadius = self.layer.cornerRadius | |
backgroundView.translatesAutoresizingMaskIntoConstraints = false | |
let top = NSLayoutConstraint(item: backgroundView, attribute: .Top, relatedBy: .Equal, toItem: self, attribute: .Top, multiplier: 1, constant: 0) | |
let right = NSLayoutConstraint(item: backgroundView, attribute: .Leading, relatedBy: .Equal, toItem: self, attribute: .Leading, multiplier: 1, constant: 0) | |
let width = NSLayoutConstraint(item: backgroundView, attribute: .Width, relatedBy: .Equal, toItem: self, attribute: .Width, multiplier: 1, constant: 0) | |
let height = NSLayoutConstraint(item: backgroundView, attribute: .Height, relatedBy: .Equal, toItem: self, attribute: .Height, multiplier: 1, constant: 0) | |
NSLayoutConstraint.activateConstraints([top, right, width, height]) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment