我的带有自初始化的 XIB 类
final class MarkerWindow: UIView {
@IBOutlet var containerView: UIView!
@IBOutlet weak var imgUser: UIImageView!
@IBOutlet weak var lblUserName: UILabel!
@IBOutlet weak var lblEmergency: UILabel!
@IBOutlet weak var lblDistance: UILabel!
@IBOutlet weak var vwEmergency: UIView!
@IBOutlet weak var widthConst: NSLayoutConstraint!
@IBOutlet weak var heightConst: NSLayoutConstraint!
override init(frame: CGRect) {
super.init(frame: frame)
self.initSubViews()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
self.initSubViews()
}
override func awakeFromNib() {
super.awakeFromNib()
vwEmergency.CornerRadius = (vwEmergency.frame.height) / 2
}
private func initSubViews() {
let nib = UINib(nibName: String(describing: type(of: self)), bundle: Bundle(for: type(of: self)))
nib.instantiate(withOwner: self, options: nil)
containerView.translatesAutoresizingMaskIntoConstraints = false
widthConst.constant = 388
heightConst.constant = 100
addSubview(containerView)
self.addConstraints()
}
private func addConstraints() {
NSLayoutConstraint.activate([
self.topAnchor.constraint(equalTo: containerView.topAnchor),
self.leadingAnchor.constraint(equalTo: containerView.leadingAnchor),
self.trailingAnchor.constraint(equalTo: containerView.trailingAnchor),
self.bottomAnchor.constraint(equalTo: containerView.bottomAnchor)
])
}
}
调用委托方法
func mapView(_ mapView: GMSMapView, markerInfoWindow marker: GMSMarker) -> UIView? {
let customInfoWindow = MarkerWindow()
return customInfoWindow
}
通过文件所有者指定的 XIB 类中连接的插座
尝试通过其他方法从视图控制器初始化 xib 时出现错误访问代码 - 2。
我在 viewdidload() 中调用了 xib 而不是信息窗口方法。
var 自定义视图 = MarkerWindow()
customView = MarkerWindow(frame: CGRect(x: 0, y: 0, 宽度: 388, 高度: 100))
然后我在方法中返回最顶层的 UIView 如下:
func mapView(_mapView:GMSMapView,markerInfoWindow标记:GMSMarker)-> UIView? {
return customView.containerView
}
而且,我现在可以看到我的 xib 视图。我还从 xib 的最顶层视图中删除了 MarkerWindow 类。现在,它仅在 FileOwner 中。