Set UIButton Layer Border Width and Color in Interface Builder(在 Interface Builder 中设置 UIButton 图层边框宽度和颜色)
问题描述
我可以使用 IB_DESIGNABLE 和/或 IBInspectable 在 Interface Builder 中设置 layer.borderWidth 和 layer.borderColor 吗?我目前正在代码中创建我的按钮,但我希望能够在 IB 中设置所有这些,但我不确定这些属性是否可以在 Xcode 6 中以这种方式设置.我想让它成为 IBOutlet而不是在代码中设置所有这些.这是我现在的按钮代码.
Can I use IB_DESIGNABLE and/or IBInspectable to set layer.borderWidth and layer.borderColor in Interface Builder? I am currently creating my button in code but I'd like to be able to set all of this in IB but I'm not sure if these properties can be set that way in Xcode 6. I'd like to make this an IBOutlet instead of having all of this set in code. Here is my button code now.
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,所以我必须在代码中设置它.
I set these values as suggested and the border is never shown in the simulator. I found out why the border wasn't showing up when setting these values in IB. The border color is a CGColor so I had to set it in code.
推荐答案
其实可以通过interface builder来设置视图层的一些属性.我知道我可以通过xcode设置一个图层的borderWidth和cornerRadius.边框颜色不起作用,可能是因为图层需要 CGColor 而不是 UIColor.
Actually you can set some properties of a view's layer through interface builder. I know that I can set a layer's borderWidth and cornerRadius through xcode. borderColor doesn't work, probably because the layer wants a CGColor instead of a UIColor.
您可能必须使用字符串而不是数字,但它确实有效!
You might have to use Strings instead of numbers, but it works!
但是您可以使用类别来代理属性,例如 layer.borderColor.(来自 ConventionalC CocoaPod)
But you can use categories to proxy properties such as layer.borderColor. (From the 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 中.
The result will be apparent during runtime, not in Xcode.
这篇关于在 Interface Builder 中设置 UIButton 图层边框宽度和颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 Interface Builder 中设置 UIButton 图层边框宽度和颜色
- 如何在 iPhone 模拟器中重置 NSUserDefaults 数据? 2022-01-01
- GPS状态的广播接收器? 2022-01-01
- 类似于 Mail.app 的 iPad 模态视图控制器? 2022-01-01
- URL编码Swift iOS 2022-01-01
- 在 Iphone SDK 的导航栏上添加多个按钮 2022-01-01
- 使用自动布局向 UIScrollView 添加动态大小的视图 2022-01-01
- UITextView 内容插图 2022-01-01
- SetOnItemSelectedListener上的微调程序错误 2022-01-01
- 网上有没有好的 UIScrollView 教程? 2022-01-01
- Xcode 7.3 中带有 UILabel 的 UIStackView 2022-01-01