Search code examples
iosswiftuiviewswift4

Border Padding for a UIView


I created a standard border around my UIView like so:

 vwGroup.layer.borderColor = UIColor.yellow.cgColor
 vwGroup.layer.borderWidth = 2.0;

but I would like to have 5px of padding/margin/spacing between the UIView, and the border that surrounds it.

Right now it just draws the border immediately around it. I'd like to push it out so there is clear space between.

Any suggestions? I suspect insets are the way to go but can't figure it out.

Thank you!


Solution

  • Insets isn't the way to go. You use inset for padding a view's internal content from its margins. For what you need your best option is to wrap your vwGroup inside another UIView and set the border in the wrapping view. Something like:

    let wrappingView = UIView(frame: someFrame)
    wrappingView.backgroundColor = .clear
    wrappingView.layer.borderColor = UIColor.yellow.cgColor
    wrappingView.layer.borderWidth = 2.0;
    wrappingView.addSubview(vwGroup)
    

    Of course this is just for you to get the big picture. You might want to set proper frames/constraints.