0

I'm working on my app which uses a graph to visualize some data. I'm using this nice Scrollable-GraphView for that: https://github.com/philackm/Scrollable-GraphView

Because this is only a class which inherits from UIScrollView, it's not possible to use it with Interface Builder (it also doesn't have the init(withCoder:) function implemented).

I now want to add a UIView with Interface Builder, which has all the necessary constraints to be sized properly at runtime. How can I "replace" that UIView with my GraphView at runtime?

This method is used for creating my graph at runtime:

private func createDarkGraph(frame: CGRect) -> ScrollableGraphView {
    let graphView = ScrollableGraphView(frame: frame)

    // ... some customization

    return graphView
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
beeef
  • 2,664
  • 4
  • 17
  • 27

1 Answers1

0

In interface builder you can drag a UIScrollView and set up constraint like normal UIScrollView, and then set the class to ScrollableGraphView, select the correct module (import!!! See this link XCODE 7.1 Swift 2 Unknown class in Interface Builder file). If you get the scrollView instance from interface builder (creating outlet .etc), it must be the instance of ScrollableGraphView. That should work just fine.

enter image description here

enter image description here

Community
  • 1
  • 1
Allen
  • 36
  • 1
  • 5
  • I tried that before, but when you use a custom view with interface builder you need to implement the *init(coder:)* initializer. But that isn't the case. – beeef Jul 14 '16 at 16:57
  • You get the error "Unknown class ScrollableGraphView in Interface Builder file swift" ? You should select the module below the `Class` field. See this link [XCODE 7.1 Swift 2 Unknown class in Interface Builder file] (http://stackoverflow.com/questions/33033129/xcode-7-1-swift-2-unknown-class-in-interface-builder-file) . After I set the module, it works. I will update my answer. – Allen Jul 14 '16 at 17:16
  • I'm doing it exactly as you're doing it. I have an IBOutlet connected to my ViewController which contains the ScrollableGraphView. But when I run my app I get the error `fatal error: init(coder:) has not been implemented: file ../MyApp/Libraries/ScrollableGraphView.swift, line 244` – beeef Jul 14 '16 at 18:18
  • And that's because `init(coder:)` only produces a fatal error. How did you do that? What module do I have to chose? I don't have one listed there.. – beeef Jul 14 '16 at 18:18
  • It's working now! I just removed the `fatal error(...)` call in the `init(coder:)` and placed `super.init(coder: aDecoder)` there. Thank you! – beeef Jul 14 '16 at 18:29
  • My ScrollableGraphView.swift file, line 244 was jsut call super.int(code: aDecoder) : ` required public init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder) }` . Maybe the version of our ScrollableGraphView.swift file are different. Happy to solve the problem . @beeef – Allen Jul 15 '16 at 02:54