[英]Can't subclass CPTGraphHostingView in CorePlot anymore (v2.3)?
我剛剛使用最新版本的 CorePlot(v2.3,我之前運行的版本 < 2.0)更新了我的應用程序。 我沒有收到任何錯誤,但是我的圖表消失了。 我曾經通過執行以下操作來繼承 CPTGraphHostingView:
final class GraphView: CPTGraphHostingView, CPTPlotDataSource {
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
configureGraph()
...
}
fileprivate func configureGraph() {
// Graph theme
graph.apply(CPTTheme(named: .plainWhiteTheme))
// Hosting view
self.hostedGraph = graph
// Plot Space
plotSpace = graph.defaultPlotSpace as! CPTXYPlotSpace
}
我注意到子類化 UIView 而不是 CPTGraphHostingView 適用於新版本:
final class GraphView: UIView, CPTPlotDataSource {
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
configureGraph()
...
}
fileprivate func configureGraph() {
// Graph theme
graph.apply(CPTTheme(named: .plainWhiteTheme))
// Hosting view
let hostingView = CPTGraphHostingView(frame: self.frame)
hostingView.hostedGraph = graph
self.addSubview(hostingView)
// Plot Space
plotSpace = graph.defaultPlotSpace as! CPTXYPlotSpace
}
在大多數情況下都很好,但是我的一個圖表位於 ScrollView (啟用分頁)上,因此在這種情況下為hostingView
獲取self.frame
並不容易。 我在這個新版本中遺漏了什么嗎? 謝謝!
在調整宿主視圖而不是frame
時使用bounds
。
let hostingView = CPTGraphHostingView(frame: self.bounds)
因此,根據 Eric 的回答,我刪除了子類並簡單地向托管視圖添加了約束:
fileprivate func configureGraph() {
// Graph theme
graph.apply(CPTTheme(named: .plainWhiteTheme))
let hostingView = CPTGraphHostingView(frame: self.bounds)
hostingView.hostedGraph = self.graph
self.addSubview(hostingView)
hostingView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
hostingView.leadingAnchor.constraint(equalTo: self.leadingAnchor, constant: 0),
hostingView.topAnchor.constraint(equalTo: self.safeAreaLayoutGuide.topAnchor, constant: 0),
hostingView.widthAnchor.constraint(equalTo: self.widthAnchor, multiplier: 1),
hostingView.heightAnchor.constraint(equalTo: self.heightAnchor, multiplier: 1)
])
// Plot Space
plotSpace = graph.defaultPlotSpace as! CPTXYPlotSpace
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.