Add bottom border line to UI TextField view in SwiftUI / Swift / Objective-C / Xamarin
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
A bottom-only border on a text field is one of the most common design patterns in modern mobile apps, inspired by Material Design's text input style. Unlike a full rectangular border, a single bottom line keeps the interface minimal while still clearly indicating where the user can type. Implementing this varies significantly across SwiftUI, UIKit (Swift), Objective-C, and Xamarin, so this article covers all four approaches with working code examples.
SwiftUI -- Overlay with Rectangle
SwiftUI does not have a built-in bottom border modifier, but you can achieve the effect with an overlay containing a positioned Rectangle:
The Rectangle is constrained to 1 point in height and pinned to the bottom edge via the alignment: .bottom parameter. You can adjust the thickness by changing the frame(height:) value and the color with foregroundColor.
For a reusable modifier, extract the logic into a ViewModifier:
Now any text field can use it:
UIKit Swift -- CALayer Sublayer
In UIKit, you add a CALayer as a sublayer to the text field's layer. The key detail is positioning the layer at the bottom of the field's bounds:
Setting borderStyle = .none removes the default UIKit border so only your custom bottom line appears. The layoutSubviews override ensures the border adjusts when the text field resizes, which is critical for Auto Layout and device rotation.
For a quick one-off without subclassing, use an extension:
Note that the extension approach does not automatically update on resize. Use the subclass approach when the text field's size can change.
Objective-C -- CALayer
The Objective-C implementation follows the same CALayer strategy. Create a UITextField subclass with a border layer:
Usage in a view controller:
Xamarin.Forms -- Custom Renderer or Effect
In Xamarin.Forms, you need a custom renderer or an Effect to modify the native control's appearance. An Effect is lighter weight and does not require subclassing:
In XAML:
For .NET MAUI (the successor to Xamarin.Forms), you can use Handlers instead of renderers, but the CALayer approach on iOS remains the same under the hood.
Styling Options
Across all frameworks, you can enhance the bottom border with a few common techniques. Change the border color on focus to highlight the active field. Animate the border thickness or color transition for a smoother user experience. In SwiftUI, animation is especially simple:
Common Pitfalls
- Forgetting to set
borderStyle = .nonein UIKit, which causes the default border to appear alongside your custom bottom line. - Not updating the border frame in
layoutSubviews, leading to misaligned borders after rotation or Auto Layout changes. - Adding a new sublayer every time the view appears instead of reusing a single layer, which stacks up invisible layers.
- Using hard-coded frame values in the extension approach that do not adapt to dynamic sizing.
- In Xamarin, not implementing
OnDetachedto clean up the added layer, which can cause visual artifacts when the effect is removed.
Summary
- In SwiftUI, use an
overlaywith aRectanglepinned toalignment: .bottomfor a clean, declarative solution. - In UIKit (Swift and Objective-C), add a
CALayersublayer and update its frame inlayoutSubviewsto handle resizing. - In Xamarin.Forms, use an Effect or custom renderer to access the native text field and apply the same CALayer technique.
- Always set
borderStyle = .noneon UIKit text fields to remove the default border before adding your custom one. - Extract the border logic into a reusable modifier, subclass, or effect so you can apply it consistently across your app.
Related reading
- Add entry to iOS .plist file via Cordova config.xml
- Add floating point value to android resources/values
- Add padding on view programmatically
- Add placeholder text inside UITextView in Swift?
- Add placeholder text inside UITextView in Swift?
- Add rounded corners to all UIImageViews
- add Shadow on UIView using swift 3
- Add swipe to delete UITableViewCell
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack 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.