我所做的是:
1) 创建了一个 .xib
文件 TranslationInfoWindow.xib
:
2) 创建包含以下内容的 TranslationInfoWindow.swift
文件:
import UIKit
class TranslationInfoWindow: UIView {
// MARK: - Initializers
override init(frame: CGRect) {
super.init(frame: frame)
setupView()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setupView()
}
// MARK: - Private Helper Methods
// Performs the initial setup.
private func setupView() {
let view = viewFromNibForClass()
view.frame = bounds
// Auto-layout stuff.
view.autoresizingMask = [
UIViewAutoresizing.flexibleWidth,
UIViewAutoresizing.flexibleHeight
]
// Show the view.
addSubview(view)
}
// Loads a XIB file into a view and returns this view.
private func viewFromNibForClass() -> UIView {
let bundle = Bundle(for: type(of: self))
let nib = UINib(nibName: String(describing: type(of: self)), bundle: bundle)
let view = nib.instantiate(withOwner: self, options: nil).first as! UIView
return view
}
@IBOutlet weak var avatarImageView: RoundedImageView!
@IBOutlet weak var titleLabel: UILabel!
@IBOutlet weak var usersLabel: UILabel!
}
3)这里我尝试初始化我的自定义 View :
func mapView(_ mapView: GMSMapView, markerInfoWindow marker: GMSMarker) -> UIView? {
// FIXME: There is a UIView but it doesn't want to be casted in TranslationInfoWindow
if let infoWindow = Bundle.main.loadNibNamed(
"TranslationInfoWindow", owner: view, options: nil)?.first as? TranslationInfoWindow {
return infoWindow
} else {
return nil
}
}
我做错了什么?
更新:
最佳答案
关于ios - 无法将从 XIB 创建的自定义 UIView 子类转换为我需要的类(仅限 UIView),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44332460/