【问题标题】:UIScrollView containing CGRect line graph not scrolling (Swift)包含 CGRect 线图的 UIScrollView 不滚动(Swift)
【发布时间】:2015-08-18 21:06:44
【问题描述】:

我正在尝试使用用户可滚动的 CGRect 绘制折线图。我已将其容器视图设置为可滚动(我想要水平滚动,以便线条可以延伸到屏幕之外),但它似乎没有任何效果。这是我实际视图控制器中的代码:

@IBOutlet weak var containerView: UIScrollView!
@IBOutlet weak var graphView: GraphView!
override func viewDidLoad() {
super.viewDidLoad()
logWeightButton.enabled = false
logWeightField.delegate = self
logWeightField.keyboardType = .NumberPad
self.view.addSubview(containerView)
containerView.addSubview(graphView)
containerView.contentSize = graphView.rectDisplay.size
pageLoad()
  }

这是声明 CGRect(在 UIView 类中)的代码

override func drawRect(rect: CGRect) {
    if dataPoints.count == 0 {
        return
    } else {
        self.drawLine(dataPoints, rect: rect, xCoordinates: nil, color: UIColor.blackColor())
        if xCoordinates.count != 0 {
            self.drawLine(actualPoints, rect: rect, xCoordinates: xCoordinates, color: UIColor.redColor())
        }
        rectDisplay = rect
    }
}

【问题讨论】:

    标签: ios swift uiscrollview drawrect


    【解决方案1】:

    发现我需要在故事板文件检查器中关闭自动布局 - 如果没有禁用,它可以覆盖滚动视图。

    我还确保启用滚动

    containerView.scrollEnabled = true
    

    并将 containerView 框架设置为

    self.view.frame
    

    我没有足够的声誉来发布图片,所以这里是:

    http://i.stack.imgur.com/ZOBJp.png

    图表现在滚动,但我的整个显示是一团糟(我相信是因为将容器框架设置为视图的大小),所以循环继续。

    最终更新:

    了解到滚动视图的框架必须小于其内容才能滚动(这就是将其设置为视图框架的原因 - 视图的宽度小于折线图的宽度),所以我将我的框架设置为

        containerView.frame = CGRect(x: 43.0, y: 147.0, width: 300.0, height: 117.0)
    

    现在一切正常。希望这对其他人有帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-08-11
      • 1970-01-01
      • 2015-03-24
      • 1970-01-01
      • 2015-05-01
      • 2018-02-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多