我可以使用 IB_DESIGNABLE 和/或 IBInspectable 在 Interface Builder 中设置 layer.borderWidth 和 layer.borderColor 吗?我目前正在代码中创建我的按钮,但我希望能够在 IB 中设置所有这些,但我不确定是否可以在 Xcode 6 中以这种方式设置这些属性。我想将其设为 IBOutlet而不是在代码中设置所有这些。现在这是我的按钮代码。
directions = [UIButton buttonWithType:UIButtonTypeRoundedRect];
directions.titleLabel.textAlignment = NSTextAlignmentCenter;
directions.titleLabel.font = [UIFont fontWithName:@"FranklinGothicStd-ExtraCond" size:20.0];
[directions setTitle:@"Directions" forState:UIControlStateNormal];
[directions setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
directions.frame = CGRectMake(20, 178, 70, 70);
directions.layer.borderWidth = 2.0f;
directions.layer.borderColor = [UIColor whiteColor].CGColor;
directions.clipsToBounds = YES;
directions.backgroundColor = [UIColor clearColor];
[directions addTarget:self action:@selector(getDirections:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:directions];
我按照建议设置了这些值,但模拟器中从未显示边框。 编辑:我发现为什么在 IB 中设置这些值时没有显示边框。边框颜色是 CGColor,所以我必须在代码中设置它。
最佳答案
实际上,您可以通过界面生成器设置 View 图层的一些属性。我知道我可以通过 xcode 设置图层的 borderWidth 和 cornerRadius。 borderColor 不起作用,可能是因为图层需要 CGColor 而不是 UIColor。
您可能必须使用字符串而不是数字,但它有效!
但是您可以使用类别来代理属性,例如 layer.borderColor。 (来自 ConventionalC CocoaPod)
CALayer+XibConfiguration.h:
#import <QuartzCore/QuartzCore.h>
#import <UIKit/UIKit.h>
@interface CALayer(XibConfiguration)
// This assigns a CGColor to borderColor.
@property(nonatomic, assign) UIColor* borderUIColor;
@end
CALayer+XibConfiguration.m:
#import "CALayer+XibConfiguration.h"
@implementation CALayer(XibConfiguration)
-(void)setBorderUIColor:(UIColor*)color
{
self.borderColor = color.CGColor;
}
-(UIColor*)borderUIColor
{
return [UIColor colorWithCGColor:self.borderColor];
}
@end
结果将在运行时显而易见,而不是在 Xcode 中。
关于ios - 在 Interface Builder 中设置 UIButton 图层边框宽度和颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26161429/