【问题标题】:Programmatically adding buttons to ScrollView (initially created with IB)以编程方式向 ScrollView 添加按钮(最初使用 IB 创建)
【发布时间】:2015-10-02 04:35:43
【问题描述】:

我在 IB 中完成了大部分工作,并使用 IB 创建了一个 UIScrollView(带有子视图),我现在想将 UIButtons 添加到(带有相应的约束)。我可以使用 IB 添加其中一些按钮,但实际上想将数百个按钮添加到单个 UIScrollView,因此使用 IB 似乎相当乏味。

因此,我想以编程方式在.swift 文件中添加按钮(可以从我拥有的 .txt 文件中复制/粘贴按钮标签)。

如何在对应的.swift文件中引用IB中创建的UIScrollView,以便添加这些按钮?请参阅下面的代码和注释:

override func viewDidLoad() {
        super.viewDidLoad()
        let nib = UINib(nibName: "KeyboardView", bundle: nil)
        let objects = nib.instantiateWithOwner(self, options: nil)
        view = objects[0] as! UIView;

    let buttonTitles = ["Test Quote 1", "Test Quote 2"]
    var buttons = createButtons(buttonTitles)
    var topRow = UIView(frame: CGRectMake(0, 0, 320, 40))

    for button in buttons {
        topRow.addSubview(button)
    }

    self.view.addSubview(topRow) // how do I add this topRow view to the ScrollView created in IB, rather than to the main View?

    addConstraints(buttons, containingView: topRow)

}

【问题讨论】:

    标签: ios xcode uiscrollview interface-builder


    【解决方案1】:

    如果您需要添加数百个按钮,也许您可​​以使用UITableView 而不是UIScrollView 然后您只需创建一个自定义UITableViewCell 并使用数据源来配置 UIButtons。

    【讨论】:

      猜你喜欢
      • 2021-08-07
      • 1970-01-01
      • 2015-11-21
      • 2016-03-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多