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 上显示来自 API 的 HTML 文本? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01