Guilherme
Guilherme

Reputation: 7949

How to remove UINavigationBar and UISearchBar hairline

I am creating an iOS 7 app in which I'd like to have a SearchBar right bellow the NavigationBar, and I wanted them both to look like a single piece. Therefore I need to tint them with the same color (done already) and remove that hairline at the bottom of the NavigationBar and at the top of the SearchBar. How can I achieve that?

Upvotes: 4

Views: 9007

Answers (5)

Nitesh
Nitesh

Reputation: 2024

use following code in AppDelegate (didFinishLaunchingWithOptions)

Swift :

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

Objective c :

    [[UINavigationBar appearance] setBackgroundImage:[[UIImage alloc] init]
                                  forBarPosition:UIBarPositionAny
                                      barMetrics:UIBarMetricsDefault];

[[UINavigationBar appearance] setShadowImage:[[UIImage alloc] init]];

Upvotes: 7

Valeriy
Valeriy

Reputation: 743

Swift 3:

self.navigationController?.navigationBar.setValue(true, forKey: "hidesShadow")

Upvotes: 6

Serhii Yakovenko
Serhii Yakovenko

Reputation: 12654

For those who interested how to implement "adventurous" approach of @Leo Natan, I added the sample code in my answer to the similar question.

Upvotes: 2

DobBobolina
DobBobolina

Reputation: 41

One way to remove the hairline on the top and bottom of a UISearchBar is to replace the background image with a stretchable one, that does not have that thin border. Simply make a square shaped png with the color of your choice, then:

[searchBar setBackgroundImage:[[UIImage imageNamed:@"SearchBarImage"] resizableImageWithCapInsets:UIEdgeInsetsMake( 10, 10, 10, 10)]];

Since the background is solid you can use pretty much whatever values you want for the insets.

Upvotes: 4

Léo Natan
Léo Natan

Reputation: 57040

Officially, this is only possible by setting the shadowImage of the navigationBar to an empty image. However, a closer look at the documentation, it is said:

For a custom shadow image to be shown, a custom background image must also be set with the setBackgroundImage:forBarMetrics: method. If the default background image is used, then the default shadow image will be used regardless of the value of this property.

By using a custom background image, you would lose the blurred background translucency.

If you feel adventurous, the "hairline" is a UIImageView that is a subview of the navigation bar. You can find it and set it as hidden. This is what Apple does in their native calendar app, for example. Remember to show it when the current view disappears.

Upvotes: 13

Related Questions