【发布时间】:2016-04-21 18:52:20
【问题描述】:
在objective-c中使用自动布局时如何给UIView添加特定颜色和粗细的边框?
【问题讨论】:
-
对于那些反对我的问题和答案的人,你能解释一下原因吗?这是一个非常有用的功能!
标签: ios objective-c uiview ios-autolayout
在objective-c中使用自动布局时如何给UIView添加特定颜色和粗细的边框?
【问题讨论】:
标签: ios objective-c uiview ios-autolayout
这个函数会为 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];
希望你能充分利用它。
【讨论】: