【发布时间】:2016-02-21 04:22:34
【问题描述】:
我对 swift 还很陌生,而且我一直以编程方式完成所有编码。我终于开始拓展业务,开始使用 Interface Builder 学习一些很棒的东西。
我在使用 drawRect(rect: CGRect) 函数创建的 UIView 类中创建了一些很酷的自定义绘图,现在我希望能够在循环中多次调用该类以在我的视图中进行布局。每当我尝试以编程方式实例化视图时,似乎 drawRect 没有被调用。我没有收到任何错误,只是没有绘制任何内容。 这是我用于布置自定义视图的代码,其中 TesterView 是我执行自定义绘图的自定义 UIView 子类:
func testView() {
let testerView:TesterView = TesterView()
self.view.addSubview(testerView)
testerView.translatesAutoresizingMaskIntoConstraints = false
let height = NSLayoutConstraint(item: testerView, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 200)
let width = NSLayoutConstraint(item: testerView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 200)
let centerX = NSLayoutConstraint(item: testerView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0)
let bottom = NSLayoutConstraint(item: testerView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0)
self.view.addConstraints([height, width, centerX, bottom])
}
我们将不胜感激任何和所有的帮助。我还没有开始尝试循环调用视图,因为我什至无法让它工作一次。我最终需要做的是循环并多次实例化该类。
【问题讨论】:
标签: ios iphone xcode swift drawrect