iOS
UINavigationBar
app development
Swift
UI design

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

UINavigationBar is a crucial component in iOS app development, providing seamless navigation across different views within an application. A common customization request among developers is hiding the default 1px bottom line, often referred to as the "shadow image." This article explores various methods to achieve this, providing detailed technical explanations and examples.

Understanding UINavigationBar

The UINavigationBar is part of the UIKit framework and is responsible for displaying items like titles, prompts, and toolbar buttons. It inherently includes a shadow image beneath its bottom edge, which serves as a decorative line separating the navigation bar from the content below.

Why Hide the 1px Bottom Line?

Design trends or specific app aesthetics may require a cleaner look, devoid of extra lines separating UI elements. Thus, removing the 1px bottom line can lead to a more modern, minimalistic appearance.

Techniques to Hide the UINavigationBar Bottom Line

1. Using Shadow Image Customization

The simplest method to eliminate the 1px line is by setting a custom shadow image. You can set it to an empty or transparent image. Below is an example:

swift
1if let navigationBar = navigationController?.navigationBar {
2    navigationBar.shadowImage = UIImage()
3    navigationBar.setBackgroundImage(UIImage(), for: .default)  // In case the shadowImage is linked to this image
4}

In this approach:

  • navigationBar.shadowImage = UIImage() effectively removes the shadow image.
  • setBackgroundImage(_:for:) is set with UIImage() to ensure the absence of a background image doesn't alter the shadow's appearance.

2. Applying a Custom Appearance

For a consistent look throughout the app, customizing the UINavigationBar's appearance in AppDelegate or SceneDelegate provides a centralized solution:

swift
1let appearance = UINavigationBarAppearance()
2appearance.configureWithOpaqueBackground()
3appearance.shadowColor = .clear  // Removes the shadow line
4UINavigationBar.appearance().standardAppearance = appearance

3. Utilization of Storyboard / Interface Builder

For those leveraging Storyboard:

  1. Select the UINavigationBar.
  2. In the Attributes Inspector, set the Image for the Shadow to a transparent image.

4. Addressing UIScrollView and Safe Area Interactions

When using UITableView or UICollectionView, a missing shadow line might inadvertently expose content directly beneath the navigation bar. Adjust the contentInset or contentInsetAdjustmentBehavior to counteract this if needed.

Example for UITableView:

swift
tableView.contentInsetAdjustmentBehavior = .never
tableView.contentInset = UIEdgeInsets(top: -1, left: 0, bottom: 0, right: 0)

5. Using Visual Effect Views

For complex designs requiring blur effects:

swift
1let blurEffect = UIBlurEffect(style: .light)
2let blurredEffectView = UIVisualEffectView(effect: blurEffect)
3blurredEffectView.frame = navigationBar.bounds
4blurredEffectView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
5navigationBar.addSubview(blurredEffectView)
6navigationBar.sendSubviewToBack(blurredEffectView)

Quick Reference Table

MethodStepsUse Case
Shadow Image CustomizationUse UINavigationBar.shadowImage = UIImage() Set a clear background imageQuick removal for individual bars
Custom AppearanceConfigure with UINavigationBarAppearance Set shadowColor to .clearGlobal style across the app
Storyboard/Interface BuilderSet Shadow image in Attributes InspectorGUI-based projects needing selective bar styles
UIScrollView/UITableView FixAdjust contentInsetAdjustmentBehavior or contentInset to manage layoutAvoid content clipping under the navigation bar
Visual Effect ViewsAdd UIVisualEffectView for blurring effects to merge navigation bar with backgroundAdvanced UI designs requiring transparency effects

Additional Considerations

  • iOS Version Compatibility: Ensure your approach targets the specific iOS versions your app supports. While UINavigationBarAppearance is efficient, it's available only from iOS 13 onward.
  • Design System Consistency: When hiding the shadow line, ensure this aligns with your app's design language.
  • Testing Across Diverse Screens: Different screen sizes and orientations might reveal unique layout issues post-customization, so thorough testing is recommended.

By mastering these techniques, developers can effectively tailor the UINavigationBar to suit aesthetic goals, delivering elegant and cohesive user interface designs.


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.