Skip to content

Instantly share code, notes, and snippets.

@aromal-sasidharan
Last active May 9, 2018 06:19
Show Gist options
  • Save aromal-sasidharan/270d83f2e0ee320f439a656f6ee0694f to your computer and use it in GitHub Desktop.
Save aromal-sasidharan/270d83f2e0ee320f439a656f6ee0694f to your computer and use it in GitHub Desktop.
Custom View ios
import UIKit
@IBDesignable //desinagble in storyboard(view in storyboard)
class SampleView: UIView {
/*
// Only override draw() if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
override func draw(_ rect: CGRect) {
// Drawing code
}
*/
override func awakeFromNib() { // user awake from nib instead of init with frame or coder
super.awakeFromNib()
nibSetup()
}
func loadViewFromNib() -> UIView? {
let bundle = Bundle(for: type(of: self))
let nib = UINib(nibName: "SampleView", bundle: bundle)
return nib.instantiate(
withOwner: self,
options: nil).first as? UIView
}
override func prepareForInterfaceBuilder() {
super.prepareForInterfaceBuilder()
nibSetup()
}
func nibSetup() {
guard let contentView = loadViewFromNib() else { return }
contentView.frame = bounds
contentView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
self.addSubview(contentView)
contentView.prepareForInterfaceBuilder()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment