【问题标题】:How to set border color of UIView at runtime [duplicate]如何在运行时设置 UIView 的边框颜色 [重复]
【发布时间】:2016-05-04 05:38:11
【问题描述】:

我需要在identity inspector 中使用User Defined Runtime Attributes 设置边框颜色UIView。默认情况下,颜色为BlackColor。如果iOS 中有任何限制,请告诉我。

【问题讨论】:

标签: ios iphone frame border-color


【解决方案1】:

这会起作用 -

UIView *view = [[UIView alloc] init];
    ...
    //Add a Your Color border 
    view.layer.borderColor = [UIColor colorWithWhite:1.0f alpha:1.0f].CGColor;
    view.layer.borderWidth = 1.0f; //make border 1px thick

【讨论】:

    【解决方案2】:

    你可以设置边框颜色,

    yourView.layer.borderColor = [UIColor orangeColor].CGColor; 
    yourView.layer.borderWidth = 1.0; //you have to give width to make border visible
    

    根据评论更新:

    您可以为视图设置运行时属性,如下图所示,

    但它会显示黑色边框,因为layer uses cgcolor 并且您无法在界面生成器中获取 cgcolor 参考。所以你不能直接将图层颜色设置为运行时属性。正如我上面提到的,最好从代码中设置。

    如果您只想从运行时属性设置颜色,那么您可以尝试Peter Deweese's answer

    希望这会有所帮助:)

    【讨论】:

    • 但我需要使用运行时约束添加边框颜色。这对我来说不是正确的答案。
    • 是什么意思?你到底想要什么?解释一下,因为我没有得到你的需要!
    • 在 XCode -> Attribute Inspector -> User Defined Runtime Attributes -> 这里我们也可以为视图设置约束。
    • 它的用户定义的运行时属性
    • 我认为attribute inspector 中没有可用的选项。我想你是从identity inspector 说的user define run time attribute。是这样吗?
    【解决方案3】:

    试试这个代码,它会帮助你:

    yourViewNAme.layer.borderWidth = 1;
        yourViewNAme.layer.borderColor = [UIColor DesiredColour].CGColor;
    

    【讨论】:

    • 但我需要使用运行时约束添加边框颜色。这对我来说不是正确的答案。
    【解决方案4】:

    请试试这个代码,

    ViewController.h

    声明

    @property (weak, nonatomic) IBOutlet UIView *searchView;

    ViewController.m

    viewDidLoad方法:

        searchView.layer.cornerRadius = 8.0f;
        searchView.layer.borderColor = [UIColor lightGrayColor].CGColor;
        searchView.layer.borderWidth = 0.8f;
        searchView.layer.shadowColor = [UIColor lightGrayColor].CGColor;
        searchView.layer.shadowOpacity = 0.8f;
        searchView.layer.shadowRadius = 3.0f;
        searchView.layer.shadowOffset =CGSizeMake(2.0f, 2.0f);
    

    希望对您有所帮助。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-11-29
    • 2014-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-24
    • 2014-08-13
    • 1970-01-01
    相关资源
    最近更新 更多