ios - UIScrollView 包含 CGRect 线图不滚动(Swift)

标签 ios swift uiscrollview drawrect

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

@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
    }
}

最佳答案

发现我需要在 Storyboard 文件检查器中关闭自动布局 - 如果未禁用它,它可以覆盖 ScrollView 。

我还确保启用滚动

containerView.scrollEnabled = true

并将containerView框架设置为

self.view.frame

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

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

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

最终更新:

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

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

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

关于ios - UIScrollView 包含 CGRect 线图不滚动(Swift),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32082546/

相关文章:

swift - 检测用户是否在打字?

ios - 尝试使用 NSUbiquitousKeyValueStore 使用 iCloud 做一些非常简单的事情

iOS:有没有办法以编程方式以特定字体设置 Storyboard的所有字体?

ios - 在 iOS 上创建 GridView

ios - 无法与 UIStepper 交互

iphone - UIScrollView 自动滚动

ios - UIScrollView 委托(delegate)方法 scrollViewWillEndDragging : not working?

ios - 用于缩放图像的 UIScrollView

ios - URLSession.shared.dataTask 接收数据的正确方式

ios - 更改 UITableViewCell 中 UIImageView 的大小