iOS – 无法获取@IBDesignable .xib文件以显示在故事板或模拟器上
我已将文件所有者设置为UserBannerView。 故事板中使用此.xib文件的UIView也设置为UserBannerView。 我没有收到任何错误,但它不会出现在模拟器或故事板中。
import UIKit @IBDesignable class UserBannerView: UIView { var view: UIView! @IBOutlet weak var userCreditsLabel: UILabel! @IBOutlet weak var userRenewDateLabel: UILabel! @IBOutlet weak var userImage: circlePP! @IBOutlet weak var userMembershipBtn: UIButton! @IBOutlet weak var userNameLabel: UILabel! var nibName:String = "UserBannerView" public override init(frame:CGRect){ super.init(frame: frame) print("UserBannerView init from frame") setup() } public required init?(coder aDecoder:NSCoder){ super.init(coder: aDecoder) print("UserBannerView init from decode") setup() } override func awakeFromNib() { print("UserBannerView awakeFromNib") // setup() } func setup() { print("UserBannerView:setup") let bundle = Bundle(for: type(of: self)) let nib = UINib(nibName: "UserBannerView", bundle: bundle) self.view = nib.instantiate(withOwner: self, options: nil).first as! UIView self.view.frame = bounds self.view.autoresizingMask = [.flexibleWidth, .flexibleHeight] self.addSubview(self.view) } }
尝试实现awakeFromNib
方法并添加prepareForInterfaceBuilder
方法
override func awakeFromNib() { super.awakeFromNib() self.setup() } override func prepareForInterfaceBuilder() { super.prepareForInterfaceBuilder() setup() }
还要确保您的类在xib文件中设置为fileOwner
希望这可以帮助