ios - UIButton 的 IB_DESIGNABLE 无法呈现实例

标签 ios ios8 uibutton xcode7

我创建了一个类UIButton类,使用IB_DESIGNABLE。但是我得到了一个奇怪的错误 Failed to render instance of RoundedCornerButton: Rendering the view take longer than 200 ms.您的绘图代码可能会遇到性能下降的问题。 我尝试了该网站上的许多建议,但仍然无法修复。请帮我找到根本原因。请查看图片了解更多详细信息(RoundedCornerButton:是 UIButton 类的类别)

enter image description here

更新:RoundedCornerButton类的代码:

.h文件

  #import <UIKit/UIKit.h>
IB_DESIGNABLE
@interface RoundedCornerButton : UIButton
@property (nonatomic) IBInspectable int cornerRadius;
@property (nonatomic) IBInspectable int borderWidth;
@property (nonatomic) IBInspectable UIColor* borderColor;
@end

.m 文件:

  #import "RoundedCornerButton.h"

@implementation RoundedCornerButton

-(void)drawRect:(CGRect)rect{
    self.layer.borderWidth = self.borderWidth;
    self.layer.borderColor = self.borderColor.CGColor;
    [self.layer setCornerRadius:self.cornerRadius];
    self.clipsToBounds = YES;

}
@end

我可以在设备上成功运行我的项目,但是我得到了上面的红色警告。(很奇怪,我使用的是Xcode 7.0)

最佳答案

你不能像那样在 drawRect 中玩你的 layer。那不是画画!绘图是关于内容的。

将该代码移到其他地方,例如 awakeFromNibprepareForInterfaceBuilder(两者),一切都会好起来的。

示例代码:

@implementation RoundedCornerButton

-(void) config {
    self.layer.borderWidth = self.borderWidth;
    self.layer.borderColor = self.borderColor.CGColor;
    [self.layer setCornerRadius:self.cornerRadius];
    self.clipsToBounds = YES;
}

-(void)prepareForInterfaceBuilder {
    [self config];
}

-(void)awakeFromNib {
    [super awakeFromNib];
    [self config];
}

@end

你可以看到它在 Xcode 7 的 IB 中运行良好:

enter image description here

关于ios - UIButton 的 IB_DESIGNABLE 无法呈现实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33294044/

相关文章:

iOS 内存警告

ios - 核心数据,仅获取 15 行,然后在事件触发器上获取接下来的 15 行

ios - 检查字符串是否已经是货币字符串?

javascript - 如何使用 jQuery/JavaScript 检测 ios 设备并仅隐藏 iPhone 和 iPad 的 Bootstrap 模式?

ios - Xcode 6.4 授权错误

Swift IAP 本地化价格

ios8 - viewWillTransitionToSize :withTransitionCoordinator: is not called

ios - 当我从纵向模式更改为横向模式时,按钮背景图像消失

ios - Swift 3 按钮文本自动调整大小

ios - Swift,自定义 UIButton 单击时不起作用