【发布时间】: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