在Interface Builder中设置UIButton图层边框宽度和颜色 [英] Set UIButton Layer Border Width and Color in Interface Builder

查看:240
本文介绍了在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.

推荐答案

实际上,您可以通过界面构建​​器设置视图图层的某些属性。我知道我可以通过xcode设置图层的borderWidth和cornerRadius。 borderColor不起作用,可能是因为图层需要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图层边框宽度和颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆