iOS
UINavigationBar
customization
app development
Swift

How to hide UINavigationBar 1px bottom line

Interview Questions practice on Codemia

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

Browse interview questions

Introduction

When developing iOS applications using UIKit, the UINavigationBar plays a crucial role in providing a structured navigation interface. However, you may want to customize its appearance occasionally, such as removing the default 1px bottom line (or shadow) to achieve a cleaner look. This line is a subtle divider between the navigation bar and the view below. This article will walk you through various approaches to hide this line using different methods.

Technical Explanation

The bottom line of the UINavigationBar is actually a shadow image, controlled by the shadowImage property. By default, this image appears as a hairline to serve as a visual divider. To hide this line, you can manipulate the UINavigationBar appearance.

Customizing UINavigationBar

Using a Transparent Shadow Image

One effective way to hide this line is by setting a transparent image as the shadowImage.

swift
navigationController?.navigationBar.shadowImage = UIImage()
navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)

Changing the Shadow Color

You can change the shadow color to transparent, which effectively hides the line.

swift
navigationController?.navigationBar.layer.shadowColor = UIColor.clear.cgColor

Use a Custom Appearance

SwiftUI Environment

For applications leveraging SwiftUI, you can make use of UINavigationBarAppearance.

swift
1let appearance = UINavigationBarAppearance()
2appearance.configureWithOpaqueBackground()
3appearance.shadowImage = UIImage()
4appearance.shadowColor = .clear
5
6UINavigationBar.appearance().standardAppearance = appearance
7UINavigationBar.appearance().scrollEdgeAppearance = appearance

Updating the App’s Entire Appearance

By setting the appearance proxy, you can control the style across your entire app:

swift
UINavigationBar.appearance().shadowImage = UIImage()
UINavigationBar.appearance().setBackgroundImage(UIImage(), for: .default)

Detailed Use of CoreGraphics

If you desire to use a CoreGraphics approach, you can manually draw the shadowImage.

swift
1UIGraphicsBeginImageContextWithOptions(CGSize(width: 1, height: 1), false, 0)
2UIColor.clear.setFill()
3UIRectFill(CGRect(x: 0, y: 0, width: 1, height: 1))
4let shadowImage = UIGraphicsGetImageFromCurrentImageContext()
5UIGraphicsEndImageContext()
6
7navigationController?.navigationBar.shadowImage = shadowImage

Additional Details and Considerations

Effects on Interactions

Reducing the bottom line might alter the perceived structure of your navigation bar, which could affect user interaction and design language. Always ensure that your design remains intuitive.

Support for Different iOS Versions

Ensure compatibility with different iOS versions by checking API availability where necessary. For backward compatibility, customize the navigation bar using appearance proxies as shown above.

Custom Themes and Styles

If your application uses multiple themes or styles, consider centralizing the navigation bar appearance customization in one place to reduce redundancy and ensure a consistent look and feel across the app.

Table Summarizing Key Points

MethodDescriptionCode Example
Transparent ImageHides the line by setting an empty image as shadownavigationController?.navigationBar.shadowImage = UIImage() navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
Shadow ColorSets the shadow color to clearnavigationController?.navigationBar.layer.shadowColor = UIColor.clear.cgColor
UINavigationBarAppearanceCustomizes appearance using SwiftUI Environment or app-wide settingsSee SwiftUI example above
CoreGraphicsUses manual drawing with CoreGraphics for fine controlSee CoreGraphics example above

Conclusion

Hiding the UINavigationBar 1px bottom line is a simple yet powerful way to refine the UI of an iOS application. With the methods outlined above, developers have several options to achieve the desired look, using appearance proxies, direct customizations, or even manual image rendering. The approach you choose should align with your app's overall design strategy and comply with user interface guidelines to maintain an intuitive user experience.


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.