[英]Swift: Add a xib into a UIView
我想添加一个自定义 UIView。 我的自定义视图的类定义是:
class UserCoinView: UIView {
@IBOutlet weak var userName: UILabel!
@IBOutlet weak var coinView: UIView!
@IBOutlet weak var coinAmount: UILabel!
@IBOutlet weak var coinIcon: UILabel!
override func drawRect(rect: CGRect) {
let smartCoins = SmartShopperUtil.getSmartShopper().smartCoins
if smartCoins != nil && smartCoins >= 0 {
coinAmount.text = String(smartCoins!)
coinView.backgroundColor = SmartShopperUtil.getSmartCoinBackgroundColor(SmartShopperUtil.getSmartShopper().smartCoins!)
}
userName.text = SmartShopperUtil.getSmartShopperNameWithFullName(SmartShopperUtil.getSmartShopper().name)
coinIcon.text = AEVIcons.AEV_SMART_COIN
}
}
我在 ViewController 中添加了一个 View 我想添加这个视图,并且我已经将这个视图的自定义类设置为 UserCoinView。 之后,我建立了与 ViewController 的连接,在这个 ViewController 中,我不知道该怎么做才能显示我的自定义 UIView。
在此先感谢您的帮助。
有几种方法可以做到这一点。
以编程方式添加为子视图。
如果您使用自动布局,更好的地方是viewDidLayoutSubviews
方法。
var myCustomView: UserCoinView? // declare variable inside your controller
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
if myCustomView == nil { // make it only once
myCustomView = NSBundle.mainBundle().loadNibNamed("UserCoinView", owner: self, options: nil).first as? UserCoinView
myCustomView.frame = ...
self.view.addSubview(myCustomView) // you can omit 'self' here
// if your app support both Portrait and Landscape orientations
// you should add constraints here
}
}
在 InterfaceBuilder 中添加为子视图。
您只需要在情节提要中为您的控制器放置一个空视图,并在 Identity Inspector 中为该视图分配您的类。 之后,如果需要,您可以将插座拖放到您的控制器类中。
至于我,我更喜欢第二种方法,因为您不需要以编程方式硬编码框架/创建约束,只需添加自动布局。
你可以试试这个
override init(frame: CGRect) {
super.init(frame: frame)
loadViewFromNib ()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
loadViewFromNib ()
}
func loadViewFromNib() {
let view = UINib(nibName: "CreditCardExperyView", bundle: NSBundle(forClass: self.dynamicType)).instantiateWithOwner(self, options: nil)[0] as! UIView
view.frame = bounds
view.autoresizingMask = [.FlexibleWidth, .FlexibleHeight]
self.addSubview(view);
}
// Call subview
let creditCardView : CreditCardExperyView = CreditCardExperyView(frame: CGRect(x: 0, y: UIScreen.mainScreen().bounds.size.height - 280, width: UIScreen.mainScreen().bounds.size.width, height: 280))
selfView.addSubview(creditCardView)
将它作为子视图添加到 UIViewController 或 UITableViewController 的内容视图(或视图控制器视图层次结构中的其他视图)。
在最新的 swift -> 例如:
let headerView = Bundle.main.loadNibNamed("SectionHeaderView", owner:
self, options: nil)?.first as? SectionHeaderView
self.view.addSubview(headerView!)
headerView?.frame = CGRect(x:0, y: 0, width: view.frame.width, height: 50.0)
我自己很新,但这应该有效。
添加:
viewControllerName.addSubview(userCoinView)
删除:
userCoinView.removeFromSuperview()
您也可以使用泛型函数。 对于项目使用,使其成为全球性的
struct GenericFunctions {
static func addXIB<T>(xibName: String) -> T? {
return Bundle.main.loadNibNamed(xibName, owner: self, options: nil)?.first as? T
}
}
使用这个通用函数,如:-
if let cell: StudentStatusTableViewCell = GenericFunctions.addXIB(xibName: "StudentStatusTableViewCell") {
return cell
} else {
return UITableViewCell()
}
泛型的好处是你可以使用这个函数来添加View 、 Tableviewcell和任何其他元素。 确保您在调用中使用类型,例如let cell: StudentStatusTableViewCell否则编译器不会推断类型。
快乐编码:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.