Last active
March 25, 2023 14:34
-
-
Save lamprosg/92baf84b6c66e0defd59e8c7410da6f6 to your computer and use it in GitHub Desktop.
(iOS) Load custom UIview in 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
//Create the xib CustomView.xib and add the View class as CustomView (this one) | |
override func awakeAfter(using aDecoder: NSCoder) -> Any? { | |
if self.subviews.isEmpty { | |
let nib = UINib(nibName: "CustomView", bundle: nil) | |
let viewArray = nib.instantiate(withOwner: nil, options: nil) | |
guard let view = viewArray.first as? CustomView else { | |
return self | |
} | |
view.translatesAutoresizingMaskIntoConstraints = false | |
//Copy contsraints | |
let heightAttr = NSLayoutConstraint.Attribute.height | |
let heightConstraint = LayoutConstraintsHelper.findConstraint(onItem: self, | |
attribute: heightAttr) | |
let heightCon = heightConstraint.constant | |
let newHeightConstraint = | |
LayoutConstraintsHelper.heightConstraint(withValue: heightCon, | |
forViewA: view, | |
toViewB: nil) | |
sizeConstraints.append(newHeightConstraint) | |
let widthAttr = NSLayoutConstraint.Attribute.height | |
let widthConstraint = LayoutConstraintsHelper.findConstraint(onItem: self, | |
attribute: widthAttr) | |
let widthCon = widthConstraint.constant | |
let newWidthConstraint = | |
LayoutConstraintsHelper.widthConstraint(withValue: widthCon, | |
forViewA: view, | |
toViewB: nil) | |
sizeConstraints.append(newWidthConstraint) | |
view.sizeConstraints = sizeConstraints | |
return view | |
} | |
return self | |
} |
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
//https://www.prolificinteractive.com/2017/06/09/xib-awakening-uniform-way-load-xibs/ | |
/* | |
1. Create custom user-interface view file (name it CustomView) | |
2. Create a custom Swift file matching the name of the xib file | |
3. Set File Owner to be your custom class | |
4. Implement init methods, with a call to xibSetup() | |
5. Add a UIView in your UIViewController (located inside your Storyboard) | |
6. Set its class to CustomView | |
7. That’s it! | |
*/ | |
extension UIView { | |
//1. Call this method from your init functions | |
/// Helper method to init and setup the view from the Nib. | |
func xibSetup() { | |
let view = loadFromNib() | |
addSubview(view) | |
stretch(view: view) | |
} | |
//2. Loads the view from the nib in the bundle | |
/// Method to init the view from a Nib. | |
/// | |
/// - Returns: Optional UIView initialized from the Nib of the same class name. | |
func loadFromNib<T: UIView>() -> T { | |
let selfType = type(of: self) | |
let bundle = Bundle(for: selfType) | |
let nibName = String(describing: selfType) | |
let nib = UINib(nibName: nibName, bundle: bundle) | |
guard let view = nib.instantiate(withOwner: self, options: nil).first as? T else { | |
fatalError(“Error loading nib with name \(nibName))” | |
} | |
return view | |
} | |
//3. Resizes the loaded view, ready for use | |
/// Stretches the input view to the UIView frame using Auto-layout | |
/// | |
/// - Parameter view: The view to stretch. | |
func stretch(view: UIView) { | |
view.translatesAutoresizingMaskIntoConstraints = false | |
NSLayoutConstraint.active([ | |
view.topAnchor.constraint(equalTo: topAnchor), | |
view.leftAnchor.constraint(equalTo: leftAnchor), | |
view.rightAnchor.constraint(equalTo: rightAnchor), | |
view.bottomAnchor.constraint(equalTo: bottomAnchor) | |
]) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@lamprosg How do I find the LayoutConstraintsHelper? Could you give me a keyword to find it?