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
IBDesignable Build Failed
提问by VishalPethani
I have Created IBDesignable
and IBInspectable
custom class to give shadow and corner radius for view
我创建IBDesignable
和IBInspectable
定制类给予阴影和圆角半径为视图
But When I assign Designable
class 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
这就是我得到的
采纳答案by VishalPethani
It was failing because the variables of IBInspectable
are used in someone else's IBDesignable
class
它失败了,因为 的变量IBInspectable
用于其他人的IBDesignable
课程
The following steps resolved the issue:
以下步骤解决了该问题:
- Rename the class
- Clean the project.
- Select storyboard, go to the Editor menu and do Refresh All Views or else select Automatic Refresh view; wait for build to be completed.
- 重命名类
- 清理项目。
- 选择情节提要,转到编辑器菜单并执行刷新所有视图或选择自动刷新视图;等待构建完成。
回答by Randika Vishman
First Try: IBDesignable Build Failed
第一次尝试:IBDesignable 构建失败
For me when I was hovering over the InterfaceBuilder
Designables: Build Failed
,
it was giving an error message saying something like
对我来说,当我将鼠标悬停在InterfaceBuilder
Designables: 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 UIView
class 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:
我也偶然发现了这个问题,并有两个可能有用的建议:
- 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".
- 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.
- 在“报告导航器”(cmd-9) 中检查“Interface Builder”构建日志,它是与正常项目构建不同的构建,那里可能有有用的错误消息,而不仅仅是“构建失败”。
- 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,然后它才能再次工作。“调试选定视图”始终处于禁用状态,并且可设计的构建始终显示为失败。应用程序编译并运行良好。