【发布时间】:2015-01-16 01:30:47
【问题描述】:
这是我第一次在 Xcode 上使用 IB_DESIGNABLE。
我有这个类来为 NSView 添加颜色。
标题
#import <Cocoa/Cocoa.h>
IB_DESIGNABLE @interface NSViewComCor : NSView
@property (nonatomic, weak) IBInspectable NSColor *backgroundColor;
@end
实施
#import "NSViewComCor.h"
@implementation NSViewComCor
@synthesize backgroundColor = _backgroundColor;
- (void)awakeFromNib {
[super awakeFromNib];
[self setWantsLayer:YES];
self.backgroundColor = [NSColor whiteColor]; //default color
}
- (NSColor *) backgroundColor
{
CGColorRef colorRef = self.layer.backgroundColor;
return [NSColor colorWithCGColor:colorRef];
}
- (void) setBackgroundColor:(NSColor *)backgroundColor
{ // color should change when changed on interface builder inspectable color box
self.layer.backgroundColor = backgroundColor.CGColor;
_backgroundColor = backgroundColor;
}
即使使用 IB_DESIGNABLE,此类在界面生成器上也不会以正确的颜色呈现...为什么?
【问题讨论】:
-
"没有用正确的颜色渲染" 这没有意义。它显示什么颜色?
-
“在界面生成器可检查颜色框上更改时颜色应该改变”。不,这不是 Inspectable 的工作方式。
-
白色。并非毫无意义:我在界面生成器上调整颜色,在界面生成器上继续为白色,在我运行应用程序时继续为白色。
标签: xcode cocoa interface-builder ibdesignable ibinspectable