我正在尝试使用此处描述的新IB_DESIGNABLE选项创建一个在Interface Builder中实时更新的自定义控件.
- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGRect myFrame = self.bounds;
CGContextSetLineWidth(context, 10);
CGRectInset(myFrame, 5,5);
[[UIColor redColor] set];
UIRectFrame(myFrame);
NSBundle *bundle = [NSBundle mainBundle];
NSString *plistPath;
plistPath = [bundle pathForResource:@"fileExample" ofType:@"plist"];
UIImage *tsliderOff = [UIImage imageNamed:@"btn_slider_off.png"];
[tsliderOff drawInRect:self.bounds];
}
当我在模拟器中运行时,我得到一个红色的盒子,我的图像在中心(正如预期的那样):

但是当我尝试使用Interface Builder时,它只显示为一个红色框(中间没有图像):

当我调试它:Editor-> Debug Selected Views时,它显示从bundle中加载的任何内容都是nil.plistPath和tsliderOff都显示为nil.
我确保btn_slider_off.png包含在Targets-> myFrameWork-> Build Phases-> Copy Bundle Resources中.
知道为什么Interface Builder在编辑期间没有看到png文件,但在运行时显示正常吗?如果我无法加载任何图像来渲染,"创建在Interface Builder中渲染的自定义视图"有点受限制...
根据rickster的解决方案进行编辑
rickster向我指出了解决方案 - 问题是文件不在mainBundle中,它们存在于[NSBundle bundleForClass:[self class]]中; 看来[UIImage imageNamed:@"btn_slider_off.png"]会自动使用mainBundle.
以下代码有效!
#if !TARGET_INTERFACE_BUILDER
NSBundle *bundle = [NSBundle mainBundle];
#else
NSBundle *bundle = …似乎在升级到Mac OS Mojave + Xcode 10之后,无法在Xcode上调试自定义视图,从编辑器 - >调试选择视图菜单(参见屏幕截图)
始终收到相同的消息(见截图):
确保"IBDesignablesAgent-iOS"尚未运行,"username"具有调试权限.
以前Xcode要求我在调试自定义视图之前输入我的凭据.现在它总是失败.我在两个不同的Mac上有问题(使用相同的开发者帐户)
是否有人遇到同样的问题?
有没有办法以有效的方式解决问题?
我试图在看什么,下面的建议在这里: