iOS - 在运行时获取UIView的框架,该框架是使用Interface Builder创建的

时间:2016-07-14 16:17:34

标签: ios swift uiview uiscrollview

我正在使用我的应用程序,该应用程序使用图形来显示某些数据。我正在使用这个漂亮的Scrollable-GraphView:https://github.com/philackm/Scrollable-GraphView

因为这只是一个继承自UIScrollView的类,所以不能将它与Interface Builder一起使用(它也没有实现init(withCoder :)函数)。

我现在想要添加一个带有Interface Builder的UIView,它具有在运行时正确调整大小的所有必要约束。如何在运行时用GraphView“替换”UIView?

此方法用于在运行时创建我的图形:

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

    // ... some customization

    return graphView
}

1 个答案:

答案 0 :(得分:0)

在界面构建器中,您可以拖动UIScrollView并像普通UIScrollView一样设置约束,然后将类设置为ScrollableGraphView,选择正确的模块(导入!!!请参阅此链接XCODE 7.1 Swift 2 Unknown class in Interface Builder file)。如果从界面构建器(创建插座.etc)获取scrollView实例,则它必须是ScrollableGraphView的实例。这应该可以正常工作。

enter image description here

enter image description here