Why this happens
UITextField’s built-in borderStyle property only supports four styles: none, line, bezel, and roundedRect. None of these draw a single line at the bottom edge. To get a Material Design–style underline you must either compose the line yourself in SwiftUI or add a custom subview / layer effect in UIKit.
How to fix it
1. SwiftUI: Compose a reusable underlined field
Create a small view that stacks a TextField over a Rectangle. This gives you full control over color, thickness, and horizontal padding.
import SwiftUI
struct UnderlinedTextField: View {
let placeholder: String
@Binding var text: String
var lineColor: Color = .gray
var lineHeight: CGFloat = 1
var horizontalPadding: CGFloat = 16
var body: some View {
VStack(spacing: 4) {
TextField(placeholder, text: $text)
.font(.body)
.padding(.vertical, 8)
Rectangle()
.fill(lineColor)
.frame(height: lineHeight)
.padding(.horizontal, horizontalPadding)
}
}
}
Usage:
struct LoginView: View {
@State private var email = ""
@State private var password = ""
var body: some View {
VStack(spacing: 20) {
UnderlinedTextField(placeholder: "Email", text: $email)
UnderlinedTextField(placeholder: "Password", text: $password)
}
.padding()
}
}
2. UIKit (Swift): One-liner using layer shadow
If you don’t need Auto Layout flexibility, a drop shadow on the text field’s layer draws a crisp bottom line without extra views.
extension UITextField {
func addBottomBorder(color: UIColor = .lightGray, height: CGFloat = 1) {
borderStyle = .none
layer.masksToBounds = false
layer.shadowColor = color.cgColor
layer.shadowOffset = CGSize(width: 0, height: height)
layer.shadowOpacity = 1
layer.shadowRadius = 0
}
}
Call it after the text field is in the view hierarchy:
emailTextField.addBottomBorder(color: .systemGray3, height: 0.5)
Note: Because this uses a shadow, the line stays correct even when the text field resizes (e.g., during rotation or Dynamic Type changes).
3. UIKit (Swift): Constrained UIView for pixel-perfect control
When you need the line to respect safe areas, custom insets, or animation, add a dedicated UIView with Auto Layout constraints.
extension UIView {
enum LinePosition { case top, bottom }
func addLine(at position: LinePosition,
color: UIColor,
thickness: CGFloat,
insets: UIEdgeInsets = .zero) {
let line = UIView()
line.backgroundColor = color
line.translatesAutoresizingMaskIntoConstraints = false
addSubview(line)
NSLayoutConstraint.activate([
line.leadingAnchor.constraint(equalTo: leadingAnchor, constant: insets.left),
line.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -insets.right),
line.heightAnchor.constraint(equalToConstant: thickness)
])
switch position {
case .top:
line.topAnchor.constraint(equalTo: topAnchor, constant: insets.top).isActive = true
case .bottom:
line.bottomAnchor.constraint(equalTo: bottomAnchor, constant: -insets.bottom).isActive = true
}
}
}
Usage:
passwordField.addLine(at: .bottom,
color: .systemBlue,
thickness: 2,
insets: UIEdgeInsets(top: 0, left: 20, bottom: 0, right: 20))
4. Objective-C: Same constrained approach
typedef NS_ENUM(NSUInteger, LinePosition) {
LinePositionTop,
LinePositionBottom
};
- (void)addLineToView:(UIView *)view
position:(LinePosition)position
color:(UIColor *)color
thickness:(CGFloat)thickness
insets:(UIEdgeInsets)insets {
UIView *line = [[UIView alloc] init];
line.backgroundColor = color;
line.translatesAutoresizingMaskIntoConstraints = NO;
[view addSubview:line];
[NSLayoutConstraint activateConstraints:@[
[line.leadingAnchor constraintEqualToAnchor:view.leadingAnchor constant:insets.left],
[line.trailingAnchor constraintEqualToAnchor:view.trailingAnchor constant:-insets.right],
[line.heightAnchor constraintEqualToConstant:thickness]
]];
if (position == LinePositionTop) {
[line.topAnchor constraintEqualToAnchor:view.topAnchor constant:insets.top].active = YES;
} else {
[line.bottomAnchor constraintEqualToAnchor:view.bottomAnchor constant:-insets.bottom].active = YES;
}
}
How to prevent it
- Prefer the SwiftUI composition approach for new SwiftUI projects—it’s declarative and adapts to layout changes automatically.
- In UIKit, use the constrained
UIViewmethod when you need the line to animate with the text field’s frame or respect custom insets. - Avoid the CALayer
borderWidth/borderColortrick on the text field itself; it draws on all four sides and cannot be limited to the bottom. - Set
borderStyle = .nonebefore adding any custom line, otherwise the system border will overlap your underline.
FAQ
Does the SwiftUI Rectangle approach work inside a Form or List?
Yes. The VStack behaves like any other view, so it works in Form, List, or a plain VStack. Just ensure the parent doesn’t clip the rectangle with .clipped().
Why does the shadow-based line disappear when the text field becomes first responder?
It shouldn’t. The shadow is drawn by the layer regardless of editing state. If you see it vanish, check that layer.masksToBounds is false and no parent view has clipsToBounds = true.
Can I animate the line color on focus change?
With the constrained UIView method, yes—animate line.backgroundColor inside a UIView.animate block. The shadow method cannot animate color smoothly because CALayer shadowColor is not an animatable property via UIKit animations.
Parts of this guide are adapted from content licensed under CC BY-SA 4.0; this article is shared under the same license.
