Created
January 23, 2020 02:31
-
-
Save mrugeshtank/06b3a90a61c2eaa84d279160d36662e1 to your computer and use it in GitHub Desktop.
load UIView from nib (xib)
This file contains 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
class MyView: UIView { | |
override init(frame: CGRect) { | |
super.init(frame: frame) | |
fromNib() | |
} | |
init() { | |
super.init(frame: CGRect.zero) | |
fromNib() | |
} | |
required init?(coder aDecoder: NSCoder) { | |
super.init(coder: aDecoder) | |
fromNib() | |
} | |
} | |
extension UIView { | |
@discardableResult | |
func fromNib<T : UIView>() -> T? { | |
guard let contentView = Bundle(for: type(of: self)).loadNibNamed(String(describing: type(of: self)), owner: self, options: nil)?.first as? T else { | |
// xib not loaded, or its top view is of the wrong type | |
return nil | |
} | |
self.addSubview(contentView) | |
contentView.translatesAutoresizingMaskIntoConstraints = false | |
contentView.layoutAttachAll() | |
return contentView | |
} | |
/// attaches all sides of the receiver to its parent view | |
func layoutAttachAll(margin : CGFloat = 0.0) { | |
let view = superview | |
layoutAttachTop(to: view, margin: margin) | |
layoutAttachBottom(to: view, margin: margin) | |
layoutAttachLeading(to: view, margin: margin) | |
layoutAttachTrailing(to: view, margin: margin) | |
} | |
/// attaches the top of the current view to the given view's top if it's a superview of the current view, or to it's bottom if it's not (assuming this is then a sibling view). | |
/// if view is not provided, the current view's super view is used | |
@discardableResult | |
func layoutAttachTop(to: UIView? = nil, margin : CGFloat = 0.0) -> NSLayoutConstraint { | |
let view: UIView? = to ?? superview | |
let isSuperview = view == superview | |
let constraint = NSLayoutConstraint(item: self, attribute: .top, relatedBy: .equal, toItem: view, attribute: isSuperview ? .top : .bottom, multiplier: 1.0, constant: margin) | |
superview?.addConstraint(constraint) | |
return constraint | |
} | |
/// attaches the bottom of the current view to the given view | |
@discardableResult | |
func layoutAttachBottom(to: UIView? = nil, margin : CGFloat = 0.0, priority: UILayoutPriority? = nil) -> NSLayoutConstraint { | |
let view: UIView? = to ?? superview | |
let isSuperview = (view == superview) || false | |
let constraint = NSLayoutConstraint(item: self, attribute: .bottom, relatedBy: .equal, toItem: view, attribute: isSuperview ? .bottom : .top, multiplier: 1.0, constant: -margin) | |
if let priority = priority { | |
constraint.priority = priority | |
} | |
superview?.addConstraint(constraint) | |
return constraint | |
} | |
/// attaches the leading edge of the current view to the given view | |
@discardableResult | |
func layoutAttachLeading(to: UIView? = nil, margin : CGFloat = 0.0) -> NSLayoutConstraint { | |
let view: UIView? = to ?? superview | |
let isSuperview = (view == superview) || false | |
let constraint = NSLayoutConstraint(item: self, attribute: .leading, relatedBy: .equal, toItem: view, attribute: isSuperview ? .leading : .trailing, multiplier: 1.0, constant: margin) | |
superview?.addConstraint(constraint) | |
return constraint | |
} | |
/// attaches the trailing edge of the current view to the given view | |
@discardableResult | |
func layoutAttachTrailing(to: UIView? = nil, margin : CGFloat = 0.0, priority: UILayoutPriority? = nil) -> NSLayoutConstraint { | |
let view: UIView? = to ?? superview | |
let isSuperview = (view == superview) || false | |
let constraint = NSLayoutConstraint(item: self, attribute: .trailing, relatedBy: .equal, toItem: view, attribute: isSuperview ? .trailing : .leading, multiplier: 1.0, constant: -margin) | |
if let priority = priority { | |
constraint.priority = priority | |
} | |
superview?.addConstraint(constraint) | |
return constraint | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment