【问题标题】:Set UIButton Layer Border Width and Color in Interface Builder在 Interface Builder 中设置 UIButton 图层边框宽度和颜色
【发布时间】:2014-11-27 11:28:16
【问题描述】:

我可以使用 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,所以我必须在代码中设置它。

【问题讨论】:

    标签: ios objective-c uibutton interface-builder xcode6


    【解决方案1】:

    您可以在界面构建器中设置大部分内容,为元素添加运行时属性:

    对于 layer.borderWidth = 2.0f;应该是:

    选择按钮并添加新属性

    keypath : layer.borderWidth

    类型:数字 价值 2

    这些更改在界面构建器中不可见,仅在运行时可见

    【讨论】:

    • 谢谢。这有帮助,但我想知道如何使用 IBInspectable 和 IB_DESIGNABLE 以便在 IB 中看到效果。
    • 你还得设置borderColor才能看到边框
    • 查看我的问题的更新。我已经设置了边框颜色。
    【解决方案2】:

    是的,你可以 在右侧点击身份检查器,你会发现像这样

    User Defined Runtime Attributes中点击+

    选择keypath 并编辑它

    这样写代码

    layer.cornerRadiusType 将类型更改为 number 并像这样设置您所需的值

    你还可以改变文字颜色等等。

    愉快的编码

    【讨论】:

    • 谢谢。这有帮助,但我想知道如何使用 IBInspectable 和 IB_DESIGNABLE 以便在 IB 中看到效果。
    • 而我刚才试了下,边框没有出现。
    • 你必须设置[button.layer setMasksToBounds:YES];
    • 我刚刚尝试设置,但仍然不显示边框
    【解决方案3】:

    其实你可以通过interface builder来设置视图层的一些属性。我知道我可以通过xcode设置一个图层的borderWidth和cornerRadius。边框颜色不起作用,可能是因为图层需要 CGColor 而不是 UIColor。

    您可能必须使用字符串而不是数字,但它确实有效!

    但是您可以使用类别来代理属性,例如 layer.borderColor。 (来自ConventionalCCocoaPod)

    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 中。

    【讨论】:

    • 对于遇到此问题的其他人,如果您想要 swift 版本,请查看:stackoverflow.com/a/27986696/130556
    • 从 Xcode 7.3 开始,您可以在用户定义的运行时属性中使用数字值设置cornerRadius。
    • 如何设置这个 porxy
    • 很简单。使一个类 CALayer+XibConfiguration.h 在该类中编写上述代码。就这么定了。您现在可以在运行时属性中使用它的接口构建器。
    猜你喜欢
    • 1970-01-01
    • 2020-03-07
    • 2014-08-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-29
    • 1970-01-01
    • 1970-01-01
    • 2018-02-11
    相关资源
    最近更新 更多