ios - 为什么我的 IBDesignable 不允许我使用 setImage : on my custom UIButton?

标签 ios xcode uiview ios8 ibdesignable

layoutSubviews 中,我正在调用 setImage() 并向其传递一个 UIImage 但它从未出现在 Interface Builder 中,但它始终当我运行程序时出现。

最佳答案

可能您正在使用此方法加载您的 UIImage:

UIImage *image = [UIImage imageNamed:@"image"];

这在界面构建器中不起作用,因为 imageNamed: 方法使用主包,但 IB 以不同的方式加载资源。尝试这样的事情:

- (void)prepareForInterfaceBuilder
{
    UIImage *image = [UIImage imageNamed:@"image" inBundle:[NSBundle bundleForClass:[self class]] compatibleWithTraitCollection:nil];
    [self setImage:image forState:UIControlStateNormal];
}

关于ios - 为什么我的 IBDesignable 不允许我使用 setImage : on my custom UIButton?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26595250/

相关文章:

ios - 表情符号未在 Swift 中显示

ios - iOS 应用程序中的生产崩溃日志

xcode - 解析查询不返回任何结果

ios - Autolayout - UIButton 的固有大小不包括标题插入

ios - UITextField 溢出

ios - UItableViewCell 更新错误的单元格标签文本

ios - 无法在 Xcode 7 Beta 3 中导入自己的框架

ios - 尝试将 #define DEBUG_MODE 添加到我的 prefix.pch 文件时出现编译器警告

ios - NS 通知混淆 Swift 3

ios - xib 和代码之间具有不同高度的 UIView