Cocoa Touch: How To Change UIView's Border Color And Thickness?

后端 未结 14 1054
心在旅途
心在旅途 2020-11-30 16:56

I saw in the inspector that I can change the background color, but I\'d like to also change the border color and thickness, is this possible?

相关标签:
14条回答
  • 2020-11-30 17:14
    view.layer.borderWidth = 1.0
    view.layer.borderColor = UIColor.lightGray.cgColor
    
    0 讨论(0)
  • 2020-11-30 17:15

    Add following @IBInspectables in UIView extension

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

    And then you should be able to set borderColor and borderWidth attributes directly from Attribute inspector. See attached image

    Attributes Inspector

    0 讨论(0)
  • 2020-11-30 17:16

    If you didn't want to edit the layer of a UIView, you could always embed the view within another view. The parent view would have its background color set to the border color. It would also be slightly larger, depending upon how wide you want the border to be.

    Of course, this only works if your view isn't transparent and you only want a single border color. The OP wanted the border in the view itself, but this may be a viable alternative.

    0 讨论(0)
  • 2020-11-30 17:18

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

    0 讨论(0)
  • 2020-11-30 17:19

    Xcode 6 update

    Since Xcode's newest version there is a better solution to this:

    With @IBInspectable you can set Attributes directly from within the Attributes Inspector.

    My Custom View @IBInspectable Attributes

    This sets the User Defined Runtime Attributes for you:

    enter image description here

    There are two approaches to set this up:

    Option 1 (with live updating in Storyboard)

    1. Create MyCustomView.
    2. This inherits from UIView.
    3. Set @IBDesignable (this makes the View update live).*
    4. Set your Runtime Attributes (border, etc.) with @IBInspectable
    5. Change your Views Class to MyCustomView
    6. Edit in Attributes Panel and see changes in Storyboard :)

    `

    @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 only works when set at the start of class MyCustomView

    Option 2 (not working since Swift 1.2, see comments)

    Extend your UIView Class:

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

    This way, your default View always has those extra editable fields in Attributes Inspector. Another advantage is that you don't have to change the class to MycustomView every time. However, one drawback to this is that you will only see your changes when you run your app.

    0 讨论(0)
  • 2020-11-30 17:19

    When I use Vladimir's CALayer solution, and on top of the view I have an animation, like a modal UINavigationController dismissing, I see a lot of glitches happening and having drawing performance issues.

    So, another way to achieve this, but without the glitches and performance loss, is to make a custom UIView and implement the drawRect message like so:

    - (void)drawRect:(CGRect)rect
    {
        CGContextRef contextRef = UIGraphicsGetCurrentContext();
        CGContextSetLineWidth(contextRef, 1);
        CGContextSetRGBStrokeColor(contextRef, 255.0, 255.0, 255.0, 1.0);
        CGContextStrokeRect(contextRef, rect);    
    }
    
    0 讨论(0)
提交回复
热议问题