从第一个XCode 6/iOS 8测试版开始,我已经做了几个月的工作了。我最喜欢的特性之一是实时渲染,可以在Swift中使用@IBDesignable标记。
我连一件活的东西都找不到。我想那一定是因为它是测试版,所以我决定等待完整的版本再试一次。但还是失败了。我当时认为,在我的代码中可能有来自beta版本的工件,所以我放弃了它,重新开始了。还是不起作用。诚然,这些错误现在稍微更具描述性。
这是我的代码:
import UIKit
@IBDesignable class MyButton : UIButton {
let UNPRESSED_COLOR = UIColor(red: 221.0/256.0, green: 249.0/256.0, blue: 14.0/256.0, alpha: 1.0)
let PRESSED_COLOR = UIColor(red: 166.0/256.0, green: 187.0/156.0, blue: 11.0/256.0, alpha: 1.0)
var buttonColorValue: UIColor
let TEXT_COLOR = UIColor(red: 72.0/256.0, green: 160.0/256.0, blue: 5.0/256.0, alpha: 1.0)
required init(coder: NSCoder) {
self.buttonColorValue = UNPRESSED_COLOR
super.init(coder: coder)
// Initialization code
self.setTitleColor(TEXT_COLOR, forState: UIControlState.Normal)
self.setTitleColor(TEXT_COLOR, forState: UIControlState.Highlighted)
self.setTitleColor(TEXT_COLOR, forState: UIControlState.Selected)
}
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
super.touchesBegan(touches, withEvent: event)
buttonColorValue = PRESSED_COLOR
self.setNeedsDisplay()
}
override func touchesEnded(touches: NSSet, withEvent event: UIEvent) {
super.touchesEnded(touches, withEvent: event)
buttonColorValue = UNPRESSED_COLOR
self.setNeedsDisplay()
}
override func touchesCancelled(touches: NSSet!, withEvent event: UIEvent!) {
super.touchesCancelled(touches, withEvent: event)
buttonColorValue = UNPRESSED_COLOR
self.setNeedsDisplay()
}
override func drawRect(rect: CGRect) {
buttonColorValue.setFill()
let ctx = UIGraphicsGetCurrentContext()
CGContextFillRect(ctx, rect)
//UIBezierPath(roundedRect: rect, cornerRadius: 5.0).fill()
}
}下面是我尝试用故事板中的一个按钮构建时所遇到的错误:
IB Designables: Failed to update auto layout status: Interface Builder Cocoa Touch Tool crashed
IB Designables: Failed to render instance of MyButton: Rendering the view took longer than 200 ms. Your drawing code may suffer from slow performance.正如您在我的代码中所看到的,我最初希望这个按钮是四舍五入的。我想这可能是造成这个问题的原因,尽管苹果会设计一种效率低下的圆形矩形绘图算法,这让我感到惊讶。我切换到简单的设置颜色和绘制矩形的原样。还是有问题。
我想(无论如何,我希望)有一件小事我做错了,因为我搜索过,没有其他人似乎有这个问题。好像是某种无限的循环?
这对我来说并不是必要的,但是让实时渲染工作对我来说将使开发更快、更容易,因为我将能够看到我的接口并测试它们,而不必运行它们。
预先感谢所有对如何解决这一问题有远见卓识的人。
发布于 2014-11-06 06:19:46
显然,我需要覆盖init(frame: CGRect)和init(code: NSCoder)。
开始工作了!如果有人能解释一下为什么这不管用,那就太好了。否则,我在这里很好。
发布于 2015-03-22 23:43:36
虽然这可能不是一个有用的答案,但我发现在为这个问题找了半个小时的解决方案之后,一个简单的关闭和重新打开Xcode就成功了--如果您还没有尝试过这一点,那就试试吧!
发布于 2016-05-12 13:26:35
确保您覆盖prepareForInterfaceBuilder来解决这个问题。
@IBDesignable
class SomeView: UITableView {
@IBInspectable var something: Int? { didSet { setup() } }
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setup()
}
override init(frame: CGRect, style: UITableViewStyle) {
super.init(frame: frame, style: style)
setup()
}
override func prepareForInterfaceBuilder() {
super.prepareForInterfaceBuilder()
setup()
}
func setup() {
// do sth
}
}https://stackoverflow.com/questions/26772729
复制相似问题