Runtime attributes border color
Asked Answered
L

5

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

Lenhard answered 17/3, 2017 at 5:39 Comment(3)
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.Groveman
If i use layer.borderColor my border has disappearedLenhard
because from IB we can only assign UIColor, we can't assign CGColor. You must have to do it programmatically.Groveman
P
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.

Porshaport answered 17/3, 2017 at 5:44 Comment(3)
If i use layer.borderColor my border has disappearedLenhard
Thanks! It is work for me. Do I need to save this layer.masksToBounds or remove it?Lenhard
You can set it to true.Porshaport
F
5

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

Floor answered 29/6, 2021 at 13:58 Comment(6)
! work for me.......but kindly share the code for easy to useUnexceptionable
This is used in Storyboard, code not written in viewDidLoad or viewWillAppear for this to workFloor
Where do you get that dark User Defined Runtime Attributes window from?Venom
@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 XcodeFloor
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.Venom
This is the answer that creatively solved the problem without requiring a computed property. https://mcmap.net/q/262749/-uiview-39-s-border-color-in-interface-builder-doesn-39-t-workVenom
V
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
https://mcmap.net/q/262749/-uiview-39-s-border-color-in-interface-builder-doesn-39-t-work

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!

Venom answered 8/7, 2022 at 16:52 Comment(0)
O
-1

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

Onetoone answered 17/3, 2017 at 5:51 Comment(0)
I
-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
Insupportable answered 17/3, 2017 at 5:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.