xcode IBDesignable 视图渲染超时

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26772729/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 06:06:25  来源:igfitidea点击:

IBDesignable View Rendering times out

iosxcoderenderingibdesignable

提问by jchitel

I've been working on an app on and off for a few months starting with the first XCode 6/iOS 8 beta release. One of my favorite features added is live rendering, made possible with the @IBDesignabletag in Swift.

从第一个 XCode 6/iOS 8 测试版开始,我已经断断续续地开发了几个月的应用程序。我最喜欢添加的功能之一是实时渲染,这可以通过@IBDesignableSwift 中的标签实现。

I haven't been able to get a single thing to live render. I figured that must have been because it was a beta release, so I decided to wait for the full release to come out to try again. It still failed. I figured then that there might be artifacts from the beta releases in my code, so I scrapped it and started fresh. It still doesn't work. Granted, the errors are slightly more descriptive now.

我一直无法获得任何东西进行实时渲染。我想那一定是因为它是 Beta 版本,所以我决定等待完整版本出来再试一次。它仍然失败了。我当时认为我的代码中可能存在 beta 版本的工件,所以我放弃了它并重新开始。它仍然不起作用。当然,这些错误现在稍微更具描述性。

Here is my code:

这是我的代码:

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()
    }
}

Here are the errors I get when I try to build with one of these buttons in my storyboard:

以下是我尝试在故事板中使用这些按钮之一进行构建时遇到的错误:

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.

As you can see in my code, I originally wanted this button to be rounded. I figured this might be the reason for this problem, though it surprised me that Apple would design a rounded rectangle drawing algorithm that inefficient. I switched to simply setting the color and drawing the rectangle as-is. Still had problems.

正如你在我的代码中看到的,我最初希望这个按钮是四舍五入的。我想这可能是这个问题的原因,尽管让我惊讶的是 Apple 会设计一个效率低下的圆角矩形绘制算法。我切换到简单地设置颜色并按原样绘制矩形。还是有问题。

I figure (I hope, anyway) that there is one small thing that I'm doing wrong, because I've googled and there is no one else that seems to have this problem. Seems like it might be some kind of infinite loop?

我想(无论如何,我希望)我做错了一件小事,因为我用谷歌搜索过,似乎没有其他人有这个问题。似乎它可能是某种无限循环?

It's not something that's necessary for me to continue, but getting live rendering to work will make development a lot faster and easier for me, because I will be able to see my interfaces and test them without having to run them.

这对我来说不是必需的,但是让实时渲染工作将使我的开发更快更容易,因为我将能够看到我的界面并测试它们而无需运行它们。

Thanks in advance to anyone who has a remote clue on how to solve this.

提前感谢任何对如何解决这个问题有远程线索的人。

回答by jchitel

Apparently I needed to override init(frame: CGRect)along with init(code: NSCoder).

显然我需要init(frame: CGRect)init(code: NSCoder).

Got it working! If anyone could care to explain why this wasn't working, that would be great. Otherwise, I'm fine here.

搞定了!如果有人愿意解释为什么这不起作用,那就太好了。否则,我在这里很好。

回答by Nathan Dries

Although this might not really be a helpful answer, I found that after looking for a solution for this for half an hour a simple close and re-open of Xcode did the trick - if you haven't tried this yet give it a go!

虽然这可能不是一个真正有用的答案,但我发现在为此寻找半小时的解决方案后,简单地关闭并重新打开 Xcode 就成功了 - 如果您还没有尝试过,请试一试!

回答by konsti

Make sure you override prepareForInterfaceBuilder to solve this.

确保覆盖 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
}
}

回答by Rasmus

Had the same problem but to a glance at this great article. It solved my problem.

有同样的问题,但看一眼这篇很棒的文章。它解决了我的问题。

http://nshipster.com/ibinspectable-ibdesignable/

http://nshipster.com/ibinspectable-ibdesignable/

In short

简而言之

To get these options in xcode

在 xcode 中获取这些选项

enter image description here

在此处输入图片说明

Make a property like this:

创建这样的属性:

@IBInspectable var cornerRadius: CGFloat = 0 {
   didSet {
       layer.cornerRadius = cornerRadius
       layer.masksToBounds = cornerRadius > 0
   }
}

回答by Michael Daw

For me, this error turned out to be due to a blocking call to a sqlite database (using CoreData) in one of my @IBInspectable properties.

对我来说,这个错误原来是由于在我的 @IBInspectable 属性之一中阻塞了对 sqlite 数据库(使用 CoreData)的调用。

My code originally queried the database and then used dispatchAsyncto set the value of a label based on the results of the query. I changed it so that the query was also launched in the dispatchAsyncblock. The error immediately went away.

我的代码最初是查询数据库,然后用于dispatchAsync根据查询结果设置标签的值。我更改了它,以便查询也在dispatchAsync块中启动。错误立即消失了。

If anyone else is having trouble with this error, I suggest checking out any code that would be executed during the design phase (constructors or computed properties marked with @IBInspetable). If there is any blocking code (network operations, database access, etc.), it might be causing the timeout error. Hope this helps.

如果其他人遇到此错误的问题,我建议检查将在设计阶段执行的任何代码(用@IBInspetable 标记的构造函数或计算属性)。如果有任何阻塞代码(网络操作、数据库访问等),则可能导致超时错误。希望这可以帮助。