8

I use defined runtime attributes in button.

layer.cornerRadius
layer.masksToBounds
layer.borderWidth

And I want to paint my border in green colour. But my code doesn’t work:

layer.borderUIColor

Borders have black colour. How to paint border in colour with runtime attributes?

enter image description here

  • are you sure for you this line of code? layer.borderUIColor is correct property for layer border color? May be this will be layer.borderColor. – Rahul Patel Mar 17 '17 at 05:42
  • 1
    If i use `layer.borderColor` my border has disappeared –  Mar 17 '17 at 05:45
  • 1
    because from IB we can only assign UIColor, we can't assign CGColor. You must have to do it programmatically. – Rahul Patel Mar 17 '17 at 05:45

5 Answers5

9

Actually, you are using wrong attribute.The, correct attribute is layer.borderColor. But again it will not work because it is type of CGColor and from IB we can only assign UIColor, we can't assign CGColor.

Eihter you can simply do it programaticlly.

Or

You can create extension with type CGColor.

Arpit Jain
  • 1,660
  • 12
  • 23
5

Order also matters for runtime attributes. I use following and it worked for me: enter image description here

Deepak Ghadi
  • 163
  • 2
  • 5
  • ! work for me.......but kindly share the code for easy to use – M Hamayun zeb Mar 14 '22 at 07:46
  • 1
    This is used in Storyboard, code not written in viewDidLoad or viewWillAppear for this to work – Deepak Ghadi Mar 14 '22 at 13:05
  • Where do you get that dark User Defined Runtime Attributes window from? – Alex Zavatone Jul 07 '22 at 22:04
  • @AlexZavatone it's background color set for entire Xcode not only for User defined attributes, if you haven't set dark mode for Xcode, it will be white by default in Xcode – Deepak Ghadi Jul 08 '22 at 07:13
  • Oh, you just have dark mode on in the Storyboard. I thought this was a popup. Thanks. If you are going to use borderUIColor, you need a computed property to return a cgColor. That prop doesn't exist on its own. – Alex Zavatone Jul 08 '22 at 13:08
  • This is the answer that creatively solved the problem without requiring a computed property. https://stackoverflow.com/a/46215487/1058199 – Alex Zavatone Jul 08 '22 at 16:31
0

Xcode 13.4, MacOS 12.4, 07082022

This is the answer that creatively solved the problem without requiring a computed property. It works with layer.borderColor in the User Defined Runtime Attributes to add a border color to any, UIView.

Łukasz-kalbarczykenter
http://stackoverflow.com/a/46215487/1058199

It traps the setting of each runtime attribute and traps for the key name of borderColor. If we have a match, it returns the cgColor value for borderColor from the supplied UIColor.

Here is my updated answer from the work of Łukasz-kalbarczykenter

All I changed was set the extension to CALayer from UIView and made sure to use self.border

import UIKit

extension CALayer {
    open override func setValue(_ value: Any?, forKey key: String) {
        guard key == "borderColor", let color = value as? UIColor else {
            super.setValue(value, forKey: key)
            return
        }
        
        self.borderColor = color.cgColor
    }
}

Next, in the storyboard, just select your UIView, press command option 4 and just add layer.borderColor as your desired UIColor to the User Defined Runtime Attributes. Then run your app and it should work!

Alex Zavatone
  • 4,106
  • 36
  • 54
-1

you should use : layer.borderColor and set layer.masksToBounds = false

Jagdeep Singh
  • 2,556
  • 3
  • 18
  • 28
-1

This will work for sure.

you can manage this through storyborad as you are doing but you are passing the wrong key here. it should be

    layer.borderColorFromUIColor
Garima Saini
  • 173
  • 8