【问题标题】:Can't subclass CPTGraphHostingView in CorePlot anymore (v2.3)?不能再将 CorePlot 中的 CPTGraphHostingView 子类化(v2.3)?
【发布时间】:2019-11-05 17:33:43
【问题描述】:

我刚刚使用最新版本的 CorePlot(v2.3,我之前运行的版本

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 并不容易。 我在这个新版本中遗漏了什么吗? 谢谢!

【问题讨论】:

    标签: ios swift uiview subclass core-plot


    【解决方案1】:

    在调整托管视图大小时使用bounds 而不是frame

    let hostingView = CPTGraphHostingView(frame: self.bounds)
    

    【讨论】:

    • 谢谢埃里克。那么子类化 CPTGraphHostingView 不再是可行的方法了吗?我还没有尝试过,但我想我必须在更改视图方向或重新调整大小时处理边界,对吧?
    • 从来没有。设置逻辑属于视图控制器。托管视图只是另一个视图。你把它放在屏幕上,就像其他任何东西一样。图表将始终填充托管视图,并在托管视图完成时自动更改大小。
    【解决方案2】:

    因此,根据 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
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-21
      • 2017-01-03
      • 2018-03-24
      • 1970-01-01
      相关资源
      最近更新 更多