UITextField
iOS
Border Color
Swift
User Interface

UITextField border color

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

Changing the border color of a UITextField is a very common UIKit customization for focus, validation, and branding. The important implementation detail is that the visible border is controlled by the text field's backing layer, so the cleanest solution is to style the layer directly and make that styling reusable.

Style the Border Through the Layer

If you want full control over border color, width, and corner radius, start by turning off the built-in border style and then configure the layer.

swift
1import UIKit
2
3let field = UITextField()
4field.borderStyle = .none
5field.layer.borderWidth = 1.0
6field.layer.cornerRadius = 8.0
7field.layer.borderColor = UIColor.systemGray3.cgColor
8field.layer.masksToBounds = true

This is the core answer to the question. borderColor takes a CGColor, which is why UIColor needs the .cgColor conversion.

Using .none for borderStyle matters because the built-in rounded or line styles can conflict visually with your custom layer border.

Make the Styling Reusable

If several screens use the same field styling, move it into an extension instead of repeating layer code everywhere.

swift
1import UIKit
2
3extension UITextField {
4    func applyBorder(color: UIColor,
5                     width: CGFloat = 1.0,
6                     radius: CGFloat = 8.0) {
7        borderStyle = .none
8        layer.borderColor = color.cgColor
9        layer.borderWidth = width
10        layer.cornerRadius = radius
11        layer.masksToBounds = true
12    }
13}

That keeps the visual rules centralized and makes later design changes cheaper.

Update Border Color for Focus and Validation

Border color often changes with editing state. For example, blue while focused, green when valid, and red when invalid.

swift
1import UIKit
2
3final class FormViewController: UIViewController, UITextFieldDelegate {
4    private let emailField = UITextField()
5
6    override func viewDidLoad() {
7        super.viewDidLoad()
8        emailField.delegate = self
9        emailField.applyBorder(color: .systemGray3)
10    }
11
12    func textFieldDidBeginEditing(_ textField: UITextField) {
13        textField.applyBorder(color: .systemBlue, width: 1.5)
14    }
15
16    func textFieldDidEndEditing(_ textField: UITextField) {
17        let valid = !(textField.text ?? "").isEmpty
18        textField.applyBorder(color: valid ? .systemGreen : .systemRed)
19    }
20}

This approach provides immediate feedback without adding extra labels or overlays for every state change.

Pay Attention to Layout and Appearance

A correctly styled border can still fail to appear if the text field has zero size or if the field is clipped by a parent view. When debugging, it helps to set a temporary background color so you can distinguish "the field exists but the border is wrong" from "the field is not laid out correctly."

Color choice also matters. Hard-coded colors may look fine in light mode and fail badly in dark mode. Semantic colors such as systemBlue, systemRed, and systemGray3 are safer defaults. For validation, color should not be the only signal. Pair it with helper text or an icon when the state is important.

Optional Border Animation

Subtle color animation can make focus changes feel smoother.

swift
1import QuartzCore
2import UIKit
3
4func animateBorder(to color: UIColor, on field: UITextField) {
5    let animation = CABasicAnimation(keyPath: "borderColor")
6    animation.fromValue = field.layer.borderColor
7    animation.toValue = color.cgColor
8    animation.duration = 0.2
9    field.layer.add(animation, forKey: "borderColor")
10    field.layer.borderColor = color.cgColor
11}

Keep the animation short. Borders are feedback, not decoration, and they should never lag behind typing.

Common Pitfalls

Leaving a built-in borderStyle active while also setting a custom layer border often creates inconsistent visuals. Use .none when customizing the layer.

Forgetting the .cgColor conversion causes type mismatch errors because the layer API does not accept UIColor directly.

Relying on color alone for important validation feedback is an accessibility problem. Pair border color changes with another cue.

Summary

  • Set UITextField border color through layer.borderColor.
  • Disable the built-in border style when you want full visual control.
  • Use reusable helpers so border styling stays consistent across screens.
  • Combine border color changes with layout checks and accessibility-aware feedback.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.