Last active
May 9, 2018 06:19
-
-
Save aromal-sasidharan/270d83f2e0ee320f439a656f6ee0694f to your computer and use it in GitHub Desktop.
Custom View ios
This file contains hidden or 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
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