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

标签 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
}

最佳答案

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

enter image description here

enter image description here

关于iOS - 在运行时获取使用 Interface Builder 创建的 UIView 框架,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38379289/

相关文章:

ios - 使用 swift 检查从图库中选择的模式 uiimage

swift - 锁定方向 iMessenger 应用程序?

ios - 获取 UIBarButtonItem 的框架返回 nil

swift - UIViewController 未在所有 iPhone 的 UIScrollView 中正确调整

iOS 8 当键盘出现时将 UIView 向上移动 |问题

ios - 在特定的 viewController 中处理 PushNotifcations

ios - 使用 FetchedResultsController 使用 Core Data 中的条目自动填充 TextField 下方的 Table View

ios - 截取 UIPickerView 的屏幕截图

ios - shouldChangeCharactersInRange 接受单个 '.'

ios - 尝试使用协议(protocol)使 View 可拖动时应用程序崩溃