Skip to content

Instantly share code, notes, and snippets.

@neoneye
Created March 11, 2016 21:58
Show Gist options
  • Save neoneye/8f3b9755bad271572411 to your computer and use it in GitHub Desktop.
Save neoneye/8f3b9755bad271572411 to your computer and use it in GitHub Desktop.
I have just ported Paul Slot's awesome "PSCustomViewFromXib" from objective C to Swift
// Inspired by Paul Slot's awesome PSCustomViewFromXib, https://github.com/PaulSolt/CustomUIView
class CustomViewFromXib: UIView {
var customView: UIView?
override init(frame: CGRect) {
super.init(frame: frame)
let className = String(self.dynamicType)
let anyObjectOrNil: AnyObject? = NSBundle.mainBundle().loadNibNamed(className, owner: self, options: nil)?.first
guard let anyObject = anyObjectOrNil else {
print("ERROR: CustomViewFromXib.init(frame:) Expected customView to be non-nil, but got nil")
return
}
guard let customView = anyObject as? UIView else {
let classNameForAnyObject = String(anyObject.dynamicType)
print("ERROR: CustomViewFromXib.init(frame:) Expected customView to be a UIView, but got: \(classNameForAnyObject)")
return
}
self.customView = customView
customView.frame = self.bounds
if CGRectIsEmpty(frame) {
self.bounds = customView.bounds
}
addSubview(customView)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
@PaulSolt
Copy link

Is this something that you could create a pull-request and wrap up into: CustomUIView with a Swift version of the project?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment