我创建了一个widget.xib
,我有自己的观点,我想添加到main.storyboard
。类widget.swift
看起来像这样:
import UIKit
@IBDesignable class Widget: UIView {
var view: UIView!
var nibName: String = "Widget"
override init(frame: CGRect) {
super.init(frame: frame)
setup()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setup()
}
func setup(){
view = loadViewFromNib()
view.frame = bounds
view.autoresizingMask = UIViewAutoresizing.FlexibleWidth
view.autoresizingMask = UIViewAutoresizing.FlexibleHeight
addSubview(view)
}
func loadViewFromNib() -> UIView {
let bundle = NSBundle(forClass: self.dynamicType)
let nib = UINib(nibName: nibName, bundle: bundle)
let view = nib.instantiateWithOwner(self, options: nil).first as! UIView
return view
}
} //end class
我的小部件视图看起来像这样:
现在,当我打开main.storyboard
时,我会在这个故事板上添加视图,然后在这个新视图中我将课程设置为Widget
我看到了所有标签,按钮但我没有'看到背景颜色。为什么?
快速查看我的main.storyboard: