ios – 在界面生成器中设置UIButton图层边框宽度和颜色

我可以使用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,所以我不得不在代码中设置它.

解决方法

实际上,您可以通过界面构建​​器设置视图层的某些属性.我知道我可以通过xcode设置一个图层的borderWidth和cornerRadius. borderColor不起作用,可能是因为该层想要一个CGColor而不是一个UIColor.

您可能需要使用字符串而不是数字,但它的作品!

但是您可以使用类别来代理属性,如layer.borderColor. (从ConventionalC可可宝)

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 – 在界面生成器中设置UIButton图层边框宽度和颜色全部内容,希望文章能够帮你解决ios – 在界面生成器中设置UIButton图层边框宽度和颜色所遇到的程序开发问题。

如果觉得来客网网站内容还不错,欢迎将来客网网站推荐给程序员好友。