我创建了一个视图 CustomView.xib 和一个专用类 CustomView.swift。我已通过添加视图并将自定义类设置为 CustomView.swift 将其添加到 Storyboard 中的 ViewController 中,并且它运行良好。我在 StoryBoard 中添加的约束按照我想要的方式调整笔尖的大小。现在我想创建它的副本,例如 tableView 中的单元格/行(例如 twitterfeed)。我怎样才能做到这一点,同时保持约束以使其正确调整大小?我想我的问题可以归结为:如何复制 StoryBoard 中创建的 UIView?我意识到随着行的添加,顶部约束可能必须改变。其次,由于这是一个笔尖,我如何更改其中的标签?该项目正在进行中GitHub https://github.com/karlml/CustomViewNib.git
任何帮助将非常感激!谢谢。
import UIKit
class ScrollViewController: UIViewController {
let scrollView = UIScrollView()
override func viewDidLoad() {
super.viewDidLoad()
let height = CGFloat(200.0)
self.scrollView.frame = self.view.bounds
self.scrollView.contentSize = CGSizeMake(self.view.bounds.size.width, height*3)
self.view.addSubview(self.scrollView)
var y = CGFloat(0.0)
for i in 0..<2 {
//DO I ADD IBOUTLET(s)?
// How do I set width of frame to the View in StoryBoard (that has constraints?
//Do I tag it, give it an ID - How can I create copies of the customView from StoryBoard
let customView = CustomView(frame: CGRectMake(0, y, 320, 200))
self.scrollView.addSubview(customView)
y += height
}
}
func createCustomView(index: Int) -> UIView {
//How do I instantiate the view here without setting the frame size?
let customView = CustomView(frame: CGRectMake(0, 0, 320, 200))
if index == 0{
customView.backgroundColor = UIColor.greenColor()
//How do I change the label text, like below?
//customView.label.text = "New York"
}
if index == 1{
customView.backgroundColor = UIColor.redColor()
//How do I change the label text, like below?
//customView.label.text = "Tokyo"
}
return customView
}
}
None
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)