17

I have a button with a border that I want to style as a UITextField.

So I tried:

self.dateButton.layer.borderColor = self.assesseeName.layer.borderColor

But that gives me a black border: enter image description here

How do I get the same color as the UITextField (top of screenshot)?

Tycho Pandelaar
  • 7,367
  • 8
  • 44
  • 70
  • 3
    Usually I take a punt and use `[[UIColor lightGrayColor] colorWithAlphaComponent:0.08]` and then adjust as necessary. – Rob Sanders Nov 28 '14 at 17:56
  • It's answered already here: [https://stackoverflow.com/a/25966955/5546312](https://stackoverflow.com/a/25966955/5546312) – D4ttatraya May 23 '17 at 15:32

7 Answers7

30

I set the border color for the button to be the standard light gray and the border width to be 0.25 and that matched

Swift 3:

myButton.layer.borderWidth = 0.25
myButton.layer.borderColor = UIColor.lightGray.cgColor
slfan
  • 8,950
  • 115
  • 65
  • 78
user7875087
  • 301
  • 3
  • 3
4

You can use the following code to apply same effect of textfield to buttton (borderwidth, bordercolor, & corner radius)

UIColor *borderColor = [UIColor colorWithRed:204.0/255.0 green:204.0/255.0 blue:204.0/255.0 alpha:1.0];

dateButton.layer.borderColor = borderColor.CGColor;   
dateButton.layer.borderWidth = 1.0;
dateButton.layer.cornerRadius = 5.0;
Pavan Gandhi
  • 1,729
  • 1
  • 22
  • 36
  • 4
    The color you have given does not match the default uitextfield border color – oyalhi May 01 '16 at 05:52
  • @oyahi : i just given color for demo purpose u can take anything that u want. by default is gray color for textfield border. – Pavan Gandhi May 02 '16 at 03:54
  • 4
    The question specifically asks for "How do I get the same color as the UITextField (top of screenshot)?" So I was expecting your answer to have the same color. – oyalhi May 02 '16 at 09:28
  • U can use replaced first line for color : UIColor *bordercolor = [UIColor cyanColor]; and remaining code as it its. – Pavan Gandhi May 02 '16 at 10:06
  • You can changes the color as u want or u can use haxstring also – Pavan Gandhi Oct 11 '16 at 03:55
2

According to the Mac's Digital Color Meter the RGB of the borders is 230, 230, 230.

AdvApp
  • 1,094
  • 1
  • 14
  • 27
1

In iOS 13 the color changes between light and dark mode

if #available(iOS 13.0, *) {
    view.layer.borderColor = UIColor(dynamicProvider: { trait in
        if trait.userInterfaceStyle == .light {
            return UIColor(white: 0, alpha: 0.2)
        } else {
            return UIColor(white: 1, alpha: 0.2)
        }
    }).cgColor
} else {

    view.layer.borderColor = UIColor(white: 0, alpha: 0.2).cgColor
}
zombie
  • 5,069
  • 3
  • 25
  • 54
0

Swift 4 Answer

Textfield default border color is gray.

button.layer.borderColor = UIColor.gray.cgColor
button.layer.borderWidth = 0.15
0

This worked for me

let borderColor = UIColor(displayP3Red: 204.0/255.0, green: 204.0/255.0, blue: 204.0/255.0, alpha: 1.0)
uitextfield.borderColor = borderColor
uitextfield.borderWidth = 0.5
uitextfield.cornerRadius = 5
-1

This worked for me in Swift 5.

let borderColour = UIColor(red:204.0/255.0, green:204.0/255.0, blue:204.0/255.0, alpha:0.4)
dateButton.layer.borderColor = borderColour.cgColor
dateButton.layer.borderWidth = 1.0
dateButton.layer.cornerRadius = 5
Curiosity
  • 1,753
  • 3
  • 25
  • 46