Skip to content

Instantly share code, notes, and snippets.

@kusalshrestha
Created June 15, 2016 17:50
Show Gist options
  • Save kusalshrestha/de434e63698ebe9df6de585eb8ec727b to your computer and use it in GitHub Desktop.
Save kusalshrestha/de434e63698ebe9df6de585eb8ec727b to your computer and use it in GitHub Desktop.
Making transparent background (Esp. for UItextField)
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