Last active
January 11, 2019 16:24
-
-
Save perlguy99/feace8804be0bbf0cb08d666d71ddeab to your computer and use it in GitHub Desktop.
PROPER way to init a custom view with a xib/nib - References: https://blog.supereasyapps.com/how-to-create-round-buttons-using-ibdesignable-on-ios-11/ and https://theswiftdev.com/2018/10/16/custom-uiview-subclass-from-a-xib-file/
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
// | |
// IconTitleTextView.swift | |
// | |
// Created by Brent Michalski on 12/23/18. | |
// Copyright © 2018 Perlguy, Inc. All rights reserved. | |
// | |
// THIS CUSTOM CLASS PROPERLY LOADS FROM A STORYBOARD!!! | |
import UIKit | |
import Font_Awesome_Swift | |
@IBDesignable | |
class CustomViewView: UIView { | |
@IBOutlet weak var containerView: UIView! | |
@IBOutlet weak var icon: UIImageView! | |
@IBOutlet weak var title: UILabel! | |
@IBOutlet weak var text: UILabel! | |
@IBInspectable var titleTextColor: UIColor { | |
get { return title.textColor } | |
set { title.textColor = newValue } | |
} | |
@IBInspectable var textTextColor: UIColor { | |
get { return text.textColor } | |
set { text.textColor = newValue } | |
} | |
required init?(coder aDecoder: NSCoder) { | |
super.init(coder: aDecoder) | |
initNib() | |
} | |
override init(frame: CGRect) { | |
super.init(frame: frame) | |
initNib() | |
} | |
private func initNib() { | |
if !self.subviews.isEmpty { return } | |
let bundle = Bundle(for: CustomViewView.self) | |
bundle.loadNibNamed(String(describing: CustomViewView.self), owner: self, options: nil) | |
self.addSubview(self.containerView) | |
self.containerView.translatesAutoresizingMaskIntoConstraints = false | |
NSLayoutConstraint.activate([ | |
self.containerView.topAnchor.constraint(equalTo: self.topAnchor), | |
self.containerView.bottomAnchor.constraint(equalTo: self.bottomAnchor), | |
self.containerView.leadingAnchor.constraint(equalTo: self.leadingAnchor), | |
self.containerView.trailingAnchor.constraint(equalTo: self.trailingAnchor) | |
]) | |
clipsToBounds = true | |
} | |
override func prepareForInterfaceBuilder() { | |
super.prepareForInterfaceBuilder() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Found this too:
https://www.kuma.cloud/blog/ios-dev-how-to-solve-failed-to-render-and-update-autolayout-status-when-using-ibdesignable/