【问题标题】:Objective-c: How to add borders to a UIView with auto layout [closed]Objective-c:如何使用自动布局向 UIView 添加边框 [关闭]
【发布时间】:2016-04-21 18:52:20
【问题描述】:

在objective-c中使用自动布局时如何给UIView添加特定颜色和粗细的边框?

【问题讨论】:

  • 对于那些反对我的问题和答案的人,你能解释一下原因吗?这是一个非常有用的功能!

标签: ios objective-c uiview ios-autolayout


【解决方案1】:

这个函数会为 UIView 的任意边框添加一个特定颜色和粗细的边框

- (void)addBorder:(UIView *)view toEdge:(UIRectEdge)edge withColor:(UIColor *)color withThickness:(float)thickness{
    UIView *border = [UIView new];
    border.backgroundColor = color;
    [border setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleBottomMargin];
    switch (edge) {
        case UIRectEdgeTop:
            border.frame = CGRectMake(0, 0, view.frame.size.width, thickness);
            break;
        case UIRectEdgeBottom:
            border.frame = CGRectMake(0, view.frame.size.height - thickness, view.frame.size.width, thickness);
            break;
        case UIRectEdgeLeft:
            border.frame = CGRectMake(0, 0, thickness, view.frame.size.height);
            break;
        case UIRectEdgeRight:
            border.frame = CGRectMake(view.frame.size.width - thickness, 0, thickness, view.frame.size.height);
            break;
        default:
            break;
    }
    [view addSubview:border];
}

用法

[self addBorder:yourView toEdge:UIRectEdgeTop withColor:[UIColor greenColor] withThickness:3.0f];

希望你能充分利用它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-22
    • 2013-07-10
    • 1970-01-01
    • 2019-12-28
    相关资源
    最近更新 更多