xcode IBDesignable 构建失败

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

IBDesignable Build Failed

iosswiftxcodeibdesignableibinspectable

提问by VishalPethani

I have Created IBDesignableand IBInspectablecustom class to give shadow and corner radius for view

我创建IBDesignableIBInspectable定制类给予阴影和圆角半径为视图

But When I assign Designableclass to view, I get Designable Build Failed

但是当我将Designable类分配给 时view,我得到 Designable Build Failed

This is my code

这是我的代码

import Foundation

import UIKit

@IBDesignable
class DesignableView: UIView {
}

@IBDesignable
class DesignableButton: UIButton {
}

@IBDesignable
class DesignableLabel: UILabel {
}

@IBDesignable
class DesignableTableView: UITableView {

}

extension UIView {

    @IBInspectable
    var cornerRadius: CGFloat {
        get {
            return layer.cornerRadius
        }
        set {
            layer.cornerRadius = newValue
        }
    }

    @IBInspectable
    var borderWidth: CGFloat {
        get {
            return layer.borderWidth
        }
        set {
            layer.borderWidth = newValue
        }
    }

    @IBInspectable
    var borderColor: UIColor? {
        get {
            if let color = layer.borderColor {
                return UIColor(cgColor: color)
            }
            return nil
        }
        set {
            if let color = newValue {
                layer.borderColor = color.cgColor
            } else {
                layer.borderColor = nil
            }
        }
    }

    @IBInspectable
    var shadowRadius: CGFloat {
        get {
            return layer.shadowRadius
        }
        set {
            layer.shadowRadius = newValue
        }
    }

    @IBInspectable
    var shadowOpacity: Float {
        get {
            return layer.shadowOpacity
        }
        set {
            layer.shadowOpacity = newValue
        }
    }

    @IBInspectable
    var shadowOffset: CGSize {
        get {
            return layer.shadowOffset
        }
        set {
            layer.shadowOffset = newValue
        }
    }

    @IBInspectable
    var shadowColor: UIColor? {
        get {
            if let color = layer.shadowColor {
                return UIColor(cgColor: color)
            }
            return nil
        }
        set {
            if let color = newValue {
                layer.shadowColor = color.cgColor
            } else {
                layer.shadowColor = nil
            }
        }
    }
}

This is what I got

这就是我得到的

error

错误

采纳答案by VishalPethani

It was failing because the variables of IBInspectableare used in someone else's IBDesignableclass

它失败了,因为 的变量IBInspectable用于其他人的IBDesignable课程

The following steps resolved the issue:

以下步骤解决了该问题:

  1. Rename the class
  2. Clean the project.
  3. Select storyboard, go to the Editor menu and do Refresh All Views or else select Automatic Refresh view; wait for build to be completed.
  1. 重命名类
  2. 清理项目。
  3. 选择情节提要,转到编辑器菜单并执行刷新所有视图或选择自动刷新视图;等待构建完成。

回答by Randika Vishman

First Try: IBDesignable Build Failed

第一次尝试:IBDesignable 构建失败

For me when I was hovering over the InterfaceBuilderDesignables: Build Failed, it was giving an error message saying something like

对我来说,当我将鼠标悬停在InterfaceBuilderDesignables: Build Failed,这是给一个错误信息说像

Cannot find any source files for the declaration or ...

找不到声明的任何源文件或...

So, I got it as a clue like that Xcode couldn't index my custom UIViewclass file so what I did is I just quit Xcode and Restarted it and then it had indexed my custom Swift class file and InterfaceBuilder was able to find it properly.

所以,我得到了一个线索,比如 Xcode 无法索引我的自定义UIView类文件,所以我所做的是我只是退出 Xcode 并重新启动它,然后它已经索引了我的自定义 Swift 类文件并且 InterfaceBuilder 能够正确找到它。

First of all try it out and then go to other options!

首先尝试一下,然后转到其他选项!

回答by WholeCheese

In my case, the designables were working fine in my storyboard until I deleted a few labels in the view that were no longer necessary to my user interface.

就我而言,可设计项在我的故事板中运行良好,直到我在视图中删除了一些对我的用户界面不再必要的标签。

There were no useful messages in either the build log or in the DiagnosticReport folder.

构建日志或 DiagnosticReport 文件夹中都没有有用的消息。

The fix for me was to delete the DerivedData folder (an Xcode "reboot") and rebuild the project. Magically, the storyboard is again showing my custom knobs! :-)

我的解决方法是删除 DerivedData 文件夹(Xcode“重新启动”)并重建项目。神奇的是,故事板再次显示了我的自定义旋钮!:-)

回答by Jan Misker

I stumbled on this issue as well, and have two suggestions that might be helpful:

我也偶然发现了这个问题,并有两个可能有用的建议:

  1. In the "Report Navigator" (cmd-9) check the "Interface Builder" build log, it's a separate build from your normal project build, there could be useful error messages there instead of just "Build failed".
  2. The Interface Builder build is done for the Simulator, i.e. x86_64 architecture. Some parts of iOS (like Metal in my case) are not present on the simulator, resulting in build failures. I defined some classes and functions inside #if targetEnvironment(simulator)blocks to at least pass the build.
  1. 在“报告导航器”(cmd-9) 中检查“Interface Builder”构建日志,它是与正常项目构建不同的构建,那里可能有有用的错误消息,而不仅仅是“构建失败”。
  2. Interface Builder 构建是为模拟器完成的,即x86_64 架构。模拟器上不存在 iOS 的某些部分(如我的情况下的 Metal),从而导致构建失败。我在#if targetEnvironment(simulator)块内定义了一些类和函数以至少通过构建。

回答by ak_ninan

If your CustomClass is added in to the project as Reference files then this occurs. That is for example you can see Blue coloured folder in the Project Explorer, that are Reference folders. If you are copying the Classes into our project then make sure that 'Copy items if needed' is selected.

如果您的 CustomClass 作为参考文件添加到项目中,则会发生这种情况。例如,您可以在项目资源管理器中看到蓝色文件夹,即参考文件夹。如果您要将类复制到我们的项目中,请确保选中“如果需要,则复制项目”。

In my case, this was the issue.

就我而言,这就是问题所在。

回答by Anup G Prasad

Even with the first 2 steps fixed mine. 1. Set the Class to DesignableView. 2. Set the Module to 3. Clean the build. 4. Restart Xcode.

即使前 2 个步骤修复了我的问题。1. 将类设置为 DesignableView。2. 将模块设置为 3. 清洁构建。4. 重启Xcode。

回答by Duncan Groenewald

I struggled with this too and then deleted projects derived data and restarted Xcode before it would work again. "Debug Selected Views" was always disabled and designable build was always showing as failed. App compiled and ran fine.

我也为此苦苦挣扎,然后删除了项目派生数据并重新启动了 Xcode,然后它才能再次工作。“调试选定视图”始终处于禁用状态,并且可设计的构建始终显示为失败。应用程序编译并运行良好。