我在inspector中看到我可以更改背景颜色,但我也想更改边框颜色和粗细,这可能吗?
您需要使用视图的图层来设置边框属性。例如:
#import <QuartzCore/QuartzCore.h>
...
view.layer.borderColor = [UIColor redColor].CGColor;
view.layer.borderWidth = 3.0f;
您还需要与 QuartzCore.framework 链接才能访问此功能。
Xcode 6 更新
由于 Xcode 的最新版本有一个更好的解决方案:
使用 @IBInspectable
,您可以直接从 in Attributes Inspector
设置属性。
https://i.stack.imgur.com/Vuoku.gif
这会为您设置 User Defined Runtime Attributes
:
https://i.stack.imgur.com/2nXiO.gif
有两种设置方法:
选项 1(在 Storyboard 中进行实时更新)
创建 MyCustomView。这继承自 UIView。设置@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
。但是,这样做的一个缺点是您只会在运行应用程序时看到您的更改。
您还可以使用您希望的颜色创建边框。
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 之间的值。
r
、g
和 b
应该是浮点数;除数也应该是浮点数:r / 255.0
。
在 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属性。见附图
当我使用 Vladimir 的 CALayer 解决方案时,在视图顶部我有一个动画,比如模态 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);
}
setNeedsDisplay
。这将强制重绘 UIView 并隐式重新调用 drawRect
。未经测试,虽然...
view.layer.borderWidth = 1.0
view.layer.borderColor = UIColor.lightGray.cgColor
试试这个代码:
view.layer.borderColor = [UIColor redColor].CGColor;
view.layer.borderWidth= 2.0;
[view setClipsToBounds:YES];
由于导致性能下降,我不建议覆盖 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 会阻止这些;-)
我想将此添加到 @marczking 的答案(选项 1)中作为评论,但我在 StackOverflow 上的低地位阻止了这一点。
我对@marczking 对Objective C 的回答做了一个移植。像魅力一样工作,谢谢@marczking!
UIView+边框.h:
#import <UIKit/UIKit.h>
IB_DESIGNABLE
@interface UIView (Border)
-(void)setBorderColor:(UIColor *)color;
-(void)setBorderWidth:(CGFloat)width;
-(void)setCornerRadius:(CGFloat)radius;
@end
UIView+边框.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
@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
}
}
如果您不想编辑 UIView 的图层,则始终可以将视图嵌入到另一个视图中。父视图将其背景颜色设置为边框颜色。它也会稍大一些,具体取决于您希望边框有多宽。
当然,这只适用于您的视图不透明并且您只需要单一边框颜色的情况。 OP 想要视图本身的边框,但这可能是一个可行的选择。
swift 4.2中项目的边框颜色:
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell_lastOrderId") as! Cell_lastOrder
cell.layer.borderWidth = 1
cell.layer.borderColor = UIColor.white.cgColor
cell.layer.cornerRadius = 10
如果你想在不同的边上添加不同的边框,可以添加一个具有特定样式的子视图是一种很容易想到的方法。
[self.view.layer setBorderColor: [UIColor colorWithRed:0.265 green:0.447 blue:0.767 alpha:1.0f].CGColor];
__bridge CGColorRef
。这不起作用。如答案中所述,它需要是[UIColor blackColor].CGColor
。#import <QuartzCore/QuartzCore.h>
。