iOS – 如何使用NIB中的特定Frame初始化自定义UIView
我想知道用特定框架初始化自定义UIView
的最简洁方法是什么。
UIView
是从XIB
文件设计的。
这是我的实施 :
class CustomView : UIView { @IBOutlet var outletLabel: UILabel! public required init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder) setupView() } public override init(frame: CGRect) { super.init(frame: frame) setupView() } private func setupView() { // Set text for labels } }
以下是我想在ViewController中初始化它的方法:
let screenSize: CGRect = UIScreen.main.bounds let screenWidth = screenSize.width let frame = CGRect(x: 0, y: 0, width: screenWidth - 50, height: 70) let customView = CustomView.init(frame: frame)
但它不起作用,我有一个没有任何sockets的白色UIView。
如果我这样做:
// Extension to UIView to load Nib let customView : CustomView = UIView.fromNib()
我可以在XIB
文件中看到我的view
,其Interface Builder
使用了它的宽度/高度。
我想用特定的框架从XIB
文件BUT加载view
是什么?
我错过了有关初始化的内容吗?
你可以有一个NibLoading类,如:
// NibLoadingView.swift //source:https://gist.github.com/winkelsdorf/16c481f274134718946328b6e2c9a4d8 import UIKit // Usage: Subclass your UIView from NibLoadView to automatically load a xib with the same name as your class @IBDesignable class NibLoadingView: UIView { @IBOutlet weak var view: UIView! override init(frame: CGRect) { super.init(frame: frame) nibSetup() } required init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder) nibSetup() } private func nibSetup() { backgroundColor = .clear view = loadViewFromNib() view.frame = bounds view.autoresizingMask = [.flexibleWidth, .flexibleHeight] view.translatesAutoresizingMaskIntoConstraints = true addSubview(view) } private func loadViewFromNib() -> UIView { let bundle = Bundle(for: type(of:self)) let nib = UINib(nibName: String(describing: type(of:self)), bundle: bundle) let nibView = nib.instantiate(withOwner: self, options: nil).first as! UIView nibView.anchorAllEdgesToSuperview() return nibView } } extension UIView { func anchorAllEdgesToSuperview() { self.translatesAutoresizingMaskIntoConstraints = false if #available(iOS 9.0, *) { addSuperviewConstraint(constraint: topAnchor.constraint(equalTo: (superview?.topAnchor)!)) addSuperviewConstraint(constraint: leftAnchor.constraint(equalTo: (superview?.leftAnchor)!)) addSuperviewConstraint(constraint: bottomAnchor.constraint(equalTo: (superview?.bottomAnchor)!)) addSuperviewConstraint(constraint: rightAnchor.constraint(equalTo: (superview?.rightAnchor)!)) } else { for attribute : NSLayoutAttribute in [.left, .top, .right, .bottom] { anchorToSuperview(attribute: attribute) } } } func anchorToSuperview(attribute: NSLayoutAttribute) { addSuperviewConstraint(constraint: NSLayoutConstraint(item: self, attribute: attribute, relatedBy: .equal, toItem: superview, attribute: attribute, multiplier: 1.0, constant: 0.0)) } func addSuperviewConstraint(constraint: NSLayoutConstraint) { superview?.addConstraint(constraint) } }
然后你的视图将NibLoadingClass子类化为:
class YourUIView: NibLoadingView { override init(frame: CGRect) { super.init(frame: frame) } required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } }
在File的Owner中设置你的XIB类,如下所示:在这种情况下,它将是YourUIView
然后实例化它:
let myView = YourUIView(frame: CGRect(x: 0, y: 0, width: self.view.frame.size.width-60, height: 170))
你可以像这样创建自定义类……
import UIKit class CustomView: UIView { class func mainView() -> CustomView { let nib = UINib(nibName: "nib", bundle: nil) let view = nib.instantiate(withOwner: self, options: nil).first as! CustomView return view } override init(frame: CGRect) { super.init(frame: frame) let view = CustomView.mainView() view.frame = frame } required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } }
初始化视图在哪里…
let view = CustomView(frame: CGRect(x: 0, y: 0, width: 100, height: 100)) view.backgroundColor = UIColor.blue self.view.addSubview(view)