ios IB Designables:无法渲染和更新自动布局状态

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46723683/
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-08-31 09:34:30  来源:igfitidea点击:

IB Designables: Failed to render and update auto layout status

iosswiftxcodeinterface-builderibdesignable

提问by Jonathan Solorzano

I have a custom view (xib) that has a UIButtoninside of it, I made id IBDesignabledoing the following:

我有一个内部有自定义视图 ( xib) UIButton,我使 idIBDesignable执行以下操作:

UserView.swift

用户视图.swift

import UIKit

@IBDesignable
class UserView: UIView {

    @IBOutlet var view: UIView!
    @IBOutlet weak var userButton: UIButton!

    override init(frame: CGRect) {
        super.init(frame: frame)
        load()

    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        load()
    }

    fileprivate func load() {
        Bundle.main.loadNibNamed("UserView", owner: self, options: nil)
        addSubview(view)
        self.frame = bounds
    }

}

UserView.xib

用户视图.xib

  • Added the UIButton and set constraints
  • Set the File's Owner: UserView
  • 添加了 UIButton 并设置约束
  • 设置文件的所有者: UserView

Storyboard

故事板

I added a UIView to a Bar Button Itemand assigned UserView class but nothing is rendering, and I got the following build error:

我添加了一个 UIView 到一个Bar Button Item并分配了 UserView 类,但没有渲染,我收到以下构建错误:

error: IB Designables: Failed to render and update auto layout status for FoodViewController (bR3-Kz-wX7): The agent threw an exception.

错误:IB Designables:无法呈现和更新 FoodViewController (bR3-Kz-wX7) 的自动布局状态:代理抛出异常。

My currently environment: Xcode 9, Swift 4

我目前的环境:Xcode 9, Swift 4

回答by Mike Velu

I just had the exact same issue, and the only thing that worked for me was instead of using the main Bundle I had to get the Bundle for the Nib I wanted. Essentially changing:

我只是遇到了完全相同的问题,唯一对我有用的是我不必使用主捆绑包,而是为我想要的笔尖获取捆绑包。本质上改变:

Bundle.main.loadNibNamed("UserView", owner: self, options: nil)

To:

到:

let bundle = Bundle(for: UserView.self)
bundle.loadNibNamed("UserView", owner: self, options: nil)

Which is bizarre, as in my case when comparing the two Bundles at runtime in LLDB they were identical (class name is different but the rest of my setup was identical to the OP)

这很奇怪,就我而言,在 LLDB 中在运行时比较两个 Bundle 时,它​​们是相同的(类名不同,但我的其余设置与 OP 相同)

lldb screenshotUpdated

lldb 截图更新

Why loading NIB from main bundle does not work?

为什么从主包加载 NIB 不起作用?

Because when InterfaceBuilder is rendering it is not running the application. There is no "main application bundle".

因为当 InterfaceBuilder 渲染时,它没有运行应用程序。没有“主应用程序包”。

回答by Haroldo Gondim

I add this script at the end of my Podfileand performed pod installagain. So I could build normally my project.

我在最后添加了这个脚本Podfilepod install再次执行。所以我可以正常构建我的项目。

post_install do |installer|
    installer.pods_project.build_configurations.each do |config|
        config.build_settings.delete('CODE_SIGNING_ALLOWED')
        config.build_settings.delete('CODE_SIGNING_REQUIRED')
    end
end

回答by Cons Bulaquena

Xcode 9.3, CocoaPods 1.5.3It may be due to recent update on CocoaPods version. See issue here.

Xcode 9.3, CocoaPods 1.5.3可能是因为最近更新了 CocoaPods 版本。请参阅此处的问题

I had this error IB Designables: Failed to render and update auto layout statusin the storyboard, the class STPPaymentCardTextField in Stripe SDK related to Cocoapods.

IB Designables: Failed to render and update auto layout status在故事板中遇到了这个错误,即与 Cocoapods 相关的 Stripe SDK 中的 STPPaymentCardTextField 类。

Solution is to downgrade your CocoaPods to 1.4.0.

解决方案是将您的 CocoaPods 降级到 1.4.0

sudo gem list cocoapods

sudo gem uninstall cocoapods

sudo gem install cocoapods -v 1.4.0

pod update

回答by Jacob F. Davis C-CISO

This question is related to this answer. Check both.
Failed to render instance of ClassName: The agent threw an exception loading nib in bundle

这个问题与这个答案有关。检查两者。
无法呈现 ClassName 的实例:代理抛出异常加载包中的笔尖

Bottom Line

底线

Make sure that your xib file does not have any orphaned outlets. Check each view/element of the nib for an outlet and remove and re-add.

确保您的 xib 文件没有任何孤立的出口。检查笔尖的每个视图/元素是否有出口,然后删除并重新添加。

A nib subview with an outlet

带插座的笔尖子视图

For me, they were orphaned outlets. It seems the nibs and IB are very finicky and the debugger doesn't give many details... if you can't get this to work, you might start with a fresh nib/xib file.

对我来说,他们是孤儿网点。看起来nibs和IB非常挑剔,调试器没有提供很多细节......如果你不能让它工作,你可以从一个新的nib/xib文件开始。



Walkthrough of my working code

我的工作代码演练

This issue took me several days to resolve. For the benefit of others I am giving a verbose answer and showing all of my relevant code and connections.

这个问题花了我好几天才解决。为了其他人的利益,我给出了一个详细的答案并展示了我所有的相关代码和连接。

A working commit where I implemented the fix action (above) is here: https://github.com/jfosterdavis/ZMAppFoundation/commit/6855f0d5b9cd1bc320395e57e2b271653ef7acd1

Xcode version 9.2

我实施修复操作(上图)的工作提交在这里:https: //github.com/jfosterdavis/ZMAppFoundation/commit/6855f0d5b9cd1bc320395e57e2b271653ef7acd1

Xcode 9.2 版

My file structure

我的文件结构

Here is my file structure (note: I am creating a pod with cocoapods named ZMAppFoundation. Also, ZMPieTimerView.swiftis not relevant to the solution.):

这是我的文件结构(注意:我正在创建一个名为 的可可豆荚ZMAppFoundation。另外,ZMPieTimerView.swift与解决方案无关。):

enter image description here

在此处输入图片说明

I want my nib to be viewable (IBDesignable) in the Main.storyboardfile.Here is the File's Owner and Custom Class of my xib, ZMGauge.xib:

我希望我的笔尖在Main.storyboard文件中可见(IBDesignable)。这是我的 xib 的文件所有者和自定义类ZMGauge.xib

File's OwnerCustom Class

文件所有者自定义类

Code, implementations

代码、实现

Here is my ZMXibView class:

这是我的 ZMXibView 类:

//  Adapted from https://medium.com/zenchef-tech-and-product/how-to-visualize-reusable-xibs-in-storyboards-using-ibdesignable-c0488c7f525d

import UIKit

@IBDesignable
open class ZMXibView: UIView {


    var contentView:UIView?
    @IBInspectable var nibName:String?

    override open func awakeFromNib() {
        super.awakeFromNib()
        xibSetup()
    }

    func xibSetup() {
        guard let view = loadViewFromNib() else { return }
        view.frame = bounds
        view.autoresizingMask =
            [.flexibleWidth, .flexibleHeight]
        addSubview(view)
        contentView = view
    }

    func loadViewFromNib() -> UIView? {
        guard let nibName = nibName else { return nil }
        let bundle = Bundle(for: type(of: self))
        let nib = UINib(nibName: nibName, bundle: bundle)
        return nib.instantiate(
            withOwner: self,
            options: nil).first as? UIView
    }

    override open func prepareForInterfaceBuilder() {
        super.prepareForInterfaceBuilder()
        xibSetup()
        contentView?.prepareForInterfaceBuilder()
    }
}

In storyboard, my xib is designated in the Custom Class. I am using cocoapods to develop a pod. I have explicitly selected the Module. (Note that in my XibView class, I have to define the nibNameproperty.):

在故事板中,我的 xib 在自定义类中指定。我正在使用 cocoapods 开发一个豆荚。我已经明确选择了模块。(请注意,在我的 XibView 类中,我必须定义nibName属性。):

Setting custom class on the xib

在 xib 上设置自定义类

Don't Forget!

不要忘记!

Then you may need to clean, close Xcode, rebuild, or simply wait a while for the IB to render it again. It is still a mystery to me exactly when Xcode will decide to try to render the IBDesignables, and I don't know of a way to force.

然后你可能需要清理、关闭 Xcode、重建,或者只是等待 IB 再次渲染它。Xcode 何时决定尝试渲染 IBDesignables 对我来说仍然是个谜,我不知道强制的方法。

Success

成功

Based on my code and the actions I took, my nib is now IBDesignable viewable in my Main.storyboard.

根据我的代码和我采取的行动,我的笔尖现在可以在 Main.storyboard 中查看 IBDesignable。

nib is viewable now

笔尖现在可见

In case you missed it

以防你错过了它

In case you missed it, my solution is at the very top of this post. I got mine to clear the error cited by the OP by removing some orphaned IBOutlets.

如果你错过了,我的解决方案在这篇文章的最顶部。我通过删除一些孤立的 IBOutlets 来清除 OP 引用的错误。

回答by olearyj234

(xCode 9.2, Swift 4)The following may not help your exact problem but may help others with a very similar problem. This was my error:

(xCode 9.2,Swift 4)以下内容可能无法帮助您解决确切的问题,但可能会帮助解决非常相似问题的其他人。这是我的错误:

error: IB Designables: Failed to render and update auto layout status for CustomExampleView (XXXXX): the agent crashed

错误:IB Designables:无法呈现和更新 CustomExampleView (XXXXX) 的自动布局状态:代理崩溃

I was able to remove the error when deleting the CustomExampleView.xib and CustomExampleView.swift. To replicate, I then re-created the files, went to the .xib and set the top level view's custom class to CustomExampleView.

删除 CustomExampleView.xib 和 CustomExampleView.swift 时,我能够消除错误。为了复制,我然后重新创建文件,转到 .xib 并将顶级视图的自定义类设置为 CustomExampleView。

My mistakewas setting the custom class to the top-level view on the .xib instead of setting the custom class on the File's Owner. After setting the proper custom class, my problem was solved.

我的错误是将自定义类设置为 .xib 上的顶级视图,而不是在File's Owner上设置自定义类。设置适当的自定义类后,我的问题就解决了。

回答by ScottyBlades

I'm cringing at how dumb this is that doing what I'm about to tell you makes a difference, but you can try removing the breaking code out of

我对做我将要告诉你的事情有所作为是多么愚蠢感到畏缩,但你可以尝试删除破坏代码

override func prepareForInterfaceBuilder() {

and only use it in

并且只在

override func awakeFromNib() {

. I'm cringing, even more, when I report that its possible that your changes could still reflect in storyboard as desired when doing this.

. 当我报告说这样做时,您的更改可能仍会根据需要反映在故事板中时,我更感到畏缩。

You can identify the breaking code by commenting out all the lines and using a process of elimination to identify the breaking line.

您可以通过注释掉所有行并使用消除过程来识别中断行来识别中断代码。

回答by FiveOhHO

For me, the fix came down to getting Interface Builder to redraw the object. I had this in two places and both were fixed by no changes to code but the symptoms of the agent error and the views being blank in storyboard was eventually fixed.

对我来说,修复归结为让 Interface Builder 重绘对象。我在两个地方都有这个问题,并且都没有通过代码更改得到修复,但是代理错误的症状和故事板中的视图空白最终得到了修复。

One was fixed by dragging a UIView to a different order under the View and rebuilding. A second in a TableView with Prototype cell was fixed by selecting the TableView, changing the count of Prototype cells to two, wait a sec, then change it back to one. The TableView, which had been a white blank, then drew all the elements correctly and the agent error was gone.

一个是通过将 UIView 拖动到 View 下的不同顺序并重建来修复的。通过选择 TableView,将 Prototype 单元格的数量更改为 2,等待一秒钟,然后将其更改回 1,可以修复带有 Prototype 单元格的 TableView 中的第二个。TableView 曾经是一个白色的空白,然后正确绘制了所有元素,代理错误消失了。

It feels like it's a bug with IB and from other searches, many solutions exist for the similar error. Hope this helps someone.

感觉这是 IB 的一个错误,从其他搜索来看,存在许多针对类似错误的解决方案。希望这可以帮助某人。

回答by Nikhil Pandey

    post_install do |installer|
    installer.pods_project.build_configurations.each do |config|
        config.build_settings.delete('CODE_SIGNING_ALLOWED')
        config.build_settings.delete('CODE_SIGNING_REQUIRED')
    end
end

Entering the above-mentioned code in the Podfile solves the issues. This code just does away with the requirement of code signing for the installed framework via Cocoapods. But this seems to be a bug in IBDesignable too as using Xcode 10 bets 4 I don't get this bug due to new build system incorporated in Xcode 10.

在 Podfile 中输入上述代码可以解决问题。这段代码只是取消了通过 Cocoapods 对已安装框架进行代码签名的要求。但这似乎也是 IBDesignable 中的一个错误,因为使用 Xcode 10 赌注 4 由于 Xcode 10 中包含了新的构建系统,我没有得到这个错误。

回答by Shaheryar Malik

It is the problem with some cocoa pods versions like 1.5.0. If you are using this one then it cause to rendering failed to move from current version to other run this commands in terminal

这是一些可可豆荚版本(如 1.5.0)的问题。如果您正在使用这个,那么它会导致渲染无法从当前版本移动到其他在终端中运行此命令

sudo gem uninstall cocoapods

You can install any specific version by mentioning like this

您可以通过这样提及来安装任何特定版本

sudo gem install cocoapods -v 1.4.0

Hope it will works for you.

希望它对你有用。

回答by Najam

For me this problem solved by,

对我来说,这个问题解决了,

  1. Updating pods.
  2. Changing build setting to '$(inherited)' for swift libraries embedded option.
  3. Building, cleaning, building.
  4. Closing project.
  5. Quitting Xcode.
  6. Running again.
  1. 更新豆荚。
  2. 将 swift 库嵌入选项的构建设置更改为 '$(inherited)'。
  3. 建造,清洁,建造。
  4. 关闭项目。
  5. 退出 Xcode。
  6. 又跑了。

hahaha old hack :) Thanks!

hahaha 老黑客 :) 谢谢!