cocoa触摸:如何更改UIView的边框颜色和厚度?
我在督察看到,我可以改变背景颜色,但我也想改变边框颜色和厚度,这可能吗?
谢谢
你需要使用视图的图层设置边框属性。 例如:
#import <QuartzCore/QuartzCore.h> ... view.layer.borderColor = [UIColor redColor].CGColor; view.layer.borderWidth = 3.0f; 您还需要链接到QuartzCore.framework来访问此function。
Xcode 6更新
由于Xcode的最新版本有一个更好的解决scheme:
 使用@IBInspectable您可以直接从Attributes Inspector设置属性。 

 这会为您设置User Defined Runtime Attributes : 

有两种方法来设置:
选项1 (在Storyboard中实时更新)
-  创buildMyCustomView。
-  这从UIViewinheritance。
-  设置@IBDesignable(这使View更新@IBDesignable)*
-  使用@IBInspectable设置运行时属性(边框等)
-  将您的视图类更改为MyCustomView
- 在“属性”面板中编辑并查看故事板中的更改:)
`
 @IBDesignable class MyCustomView: UIView { @IBInspectable var cornerRadius: CGFloat = 0 { didSet { layer.cornerRadius = cornerRadius layer.masksToBounds = cornerRadius > 0 } } @IBInspectable var borderWidth: CGFloat = 0 { didSet { layer.borderWidth = borderWidth } } @IBInspectable var borderColor: UIColor? { didSet { layer.borderColor = borderColor?.CGColor } } } 
  * @IBDesignable只能在class MyCustomView的开始处设置 
选项2 (从Swift 1.2开始工作,请参阅注释)
扩展你的UIView类:
 extension UIView { @IBInspectable var cornerRadius: CGFloat = 0 { didSet { layer.cornerRadius = cornerRadius layer.masksToBounds = cornerRadius > 0 } } @IBInspectable var borderWidth: CGFloat = 0 { didSet { layer.borderWidth = borderWidth } } @IBInspectable var borderColor: UIColor? { didSet { layer.borderColor = borderColor?.CGColor } } } 
 这样,您的默认视图始终在“ Attributes Inspector具有这些额外的可编辑字段。 另一个好处是,你不必每次都将类更改为MycustomView 。 但是,这样做的一个缺点是,只有在运行应用程序时才会看到更改。 
你也可以用你希望的颜色创build边框。
 view.layer.borderColor = [UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:1.0].CGColor; 
* r,g,b是0到255之间的值。
当我使用弗拉基米尔的CALayer解决scheme,并在视图的顶部,我有一个animation,像一个模态的UINavigationController解散,我看到很多的小故障发生,并绘制性能问题。
 所以,另一种实现这一点的方法是,没有毛病和性能损失,就是制作一个自定义的UIView,并像下面这样实现drawRect消息: 
 - (void)drawRect:(CGRect)rect { CGContextRef contextRef = UIGraphicsGetCurrentContext(); CGContextSetLineWidth(contextRef, 1); CGContextSetRGBStrokeColor(contextRef, 255.0, 255.0, 255.0, 1.0); CGContextStrokeRect(contextRef, rect); } 
在UIView扩展中添加以下@IBInspectables
 extension UIView { @IBInspectable var borderWidth: CGFloat { get { return layer.borderWidth } set(newValue) { layer.borderWidth = newValue } } @IBInspectable var borderColor: UIColor? { get { if let color = layer.borderColor { return UIColor(CGColor: color) } return nil } set(newValue) { layer.borderColor = newValue?.CGColor } } } 
然后,您应该能够直接从属性检查器设置borderColor和borderWidth属性。 见附图
属性检查器
我不会build议覆盖drawRect由于造成性能打击。
相反,我会修改像下面的类的属性(在你的自定义uiview中):
  - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { self.layer.borderWidth = 2.f; self.layer.borderColor = [UIColor redColor].CGColor; } return self; 
采取上述方法时,我没有看到任何毛病 – 不知道为什么放在initWithFrame停止这些;-)
试试这个代码:
 view.layer.borderColor = [UIColor redColor].CGColor; view.layer.borderWidth= 2.0; [view setClipsToBounds:YES]; 
我想把这个添加到@ marczking的答案( 选项1 )作为注释,但是我在StackOverflow上的低级状态阻止了这一点。
我做了@ marczking对Objective C的回答。像魅力一样工作,感谢@marczking!
UIView的+ Border.h:
 #import <UIKit/UIKit.h> IB_DESIGNABLE @interface UIView (Border) -(void)setBorderColor:(UIColor *)color; -(void)setBorderWidth:(CGFloat)width; -(void)setCornerRadius:(CGFloat)radius; @end 
UIView的+ Border.m:
 #import "UIView+Border.h" @implementation UIView (Border) // Note: cannot use synthesize in a Category -(void)setBorderColor:(UIColor *)color { self.layer.borderColor = color.CGColor; } -(void)setBorderWidth:(CGFloat)width { self.layer.borderWidth = width; } -(void)setCornerRadius:(CGFloat)radius { self.layer.cornerRadius = radius; self.layer.masksToBounds = radius > 0; } @end 
如果您不想编辑UIView的图层,则可以将视图embedded到另一个视图中。 父视图将其背景颜色设置为边框颜色。 它也会稍微大些,这取决于你想要边框的宽度。
当然,这只有在你的视图不透明并且你只需要一个边框颜色时才有效。 OP想要在视图本身的边界,但这可能是一个可行的select。
@IBInspectable在iOS 9,Swift 2.0上为我工作
 extension UIView { @IBInspectable var borderWidth: CGFloat { get { return layer.borderWidth } set(newValue) { layer.borderWidth = newValue } } @IBInspectable var cornerRadius: CGFloat { get { return layer.cornerRadius } set(newValue) { layer.cornerRadius = newValue } } @IBInspectable var borderColor: UIColor? { get { if let color = layer.borderColor { return UIColor(CGColor: color) } return nil } set(newValue) { layer.borderColor = newValue?.CGColor } } 
如果你想在不同的边添加不同的边框,可能会添加一个具有特定风格的子视图是一种容易想出的方式。
 view.layer.borderWidth = 1.0 view.layer.borderColor = UIColor.lightGray.cgColor