时间:2010-07-25 18:07:22

标签: ios objective-c iphone cocoa-touch interface-builder

14 个答案:

答案 0 :(得分:585)

答案 1 :(得分:40)

Xcode 6更新

自Xcode的最新版本以来,有一个更好的解决方案:

使用@IBInspectable,您可以直接从 Attributes Inspector中设置属性。

My Custom View @IBInspectable Attributes

这会为您设置User Defined Runtime Attributes

enter image description here

有两种设置方法:

选项1 (在故事板中进行实时更新)

  1. 创建MyCustomView
  2. 这是继承自UIView
  3. 设置@IBDesignable(这会使View更新生效)。*
  4. 使用@IBInspectable
  5. 设置运行时属性(边框等)
  6. 将您的视图类更改为MyCustomView
  7. 在“属性”面板中编辑并查看故事板中的更改:)
  8. `

    @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
            }
        }
    }
    

    这样,您的默认View 始终Attributes Inspector中包含那些额外的可修改字段。另一个优点是您不必每次都将类更改为MycustomView。 但是,这样做的一个缺点是,您只能在运行应用时看到更改。

答案 2 :(得分:16)

您还可以使用您希望的颜色创建边框..

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之间的值。

答案 3 :(得分:10)

在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属性。见附图

Attributes Inspector

答案 4 :(得分:8)

当我使用弗拉基米尔的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);    
}

答案 5 :(得分:7)

view.layer.borderWidth = 1.0
view.layer.borderColor = UIColor.lightGray.cgColor

答案 6 :(得分:6)

试试这段代码:

view.layer.borderColor =  [UIColor redColor].CGColor;
view.layer.borderWidth= 2.0;
[view setClipsToBounds:YES];

答案 7 :(得分:4)

我不建议覆盖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会停止这些; - )

答案 8 :(得分:3)

我想将此添加到@ 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

答案 9 :(得分:2)

@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
    }
}

答案 10 :(得分:1)

如果您不想编辑UIView的图层,则可以始终将视图嵌入到另一个视图中。父视图的背景颜色将设置为边框颜色。它也会略大,取决于你想要边界的宽度。

当然,这仅适用于您的视图不透明并且您只需要单一边框颜色的情况。 OP希望视图中有边框,但这可能是一个可行的选择。

答案 11 :(得分:0)

如果你想在不同的边上添加不同的边框,可能会添加一个具有特定风格的子视图,这是一种很容易想到的方法。

答案 12 :(得分:0)

迅速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

答案 13 :(得分:0)

[self.view.layer setBorderColor:[UIColor colorWithRed:0.265 green:0.447 blue:0.767 alpha:1.0f] .CGColor];

相关问题