43

I implemented a custom UITabBar and I still have this gradient/shadow on top of it. I added

[self.tabBar setBackgroundImage:[UIImage imageNamed:@"navBarBottom.png"]];

which is just changing the background but keeping the shadow gradient.

What am I doing wrong ? Is there anything to specify to get rid of it ?

What I have :

top shadow

What I want :

without shadow

Thank you.

Undo
  • 25,519
  • 37
  • 106
  • 129
httpete
  • 5,875
  • 4
  • 32
  • 41

15 Answers15

126

Similary in answer for this question ... if You don't want to mess with any kind of 1x1 transparent image, this work's too:

[[UITabBar appearance] setBackgroundImage:[[UIImage alloc] init]]; 
[[UITabBar appearance] setShadowImage:[[UIImage alloc] init]];

In swift:

UITabBar.appearance().shadowImage = UIImage()
UITabBar.appearance().backgroundImage = UIImage()
Community
  • 1
  • 1
JakubKnejzlik
  • 6,363
  • 3
  • 40
  • 41
  • 8
    Don't forget: [[UITabBar appearance] setBackgroundImage:[[UIImage alloc] init]]; – Roger Fernandez Guri Jul 28 '13 at 22:33
  • @Roger: I'm not sure, is it necessary if You just need to remove the shadow? – JakubKnejzlik Aug 03 '13 at 12:16
  • 1
    Yes. I had to do it to fully remove the shadow otherwise it still displaying it. – Roger Fernandez Guri Aug 03 '13 at 15:54
  • @RogerFernandezGuri, actually it is not still displaying shadow, but what you see is a tab bar background colour. Instead of '[[UITabBar appearance] setBackgroundImage:[[UIImage alloc] init]];', you could also try setting the background colour... – Despotovic Jan 29 '14 at 13:58
  • If you are setting the tab bar background image to custom one, then add: '[[UITabBar appearance] setBackgroundColor:[UIColor clearColor]];'. – Despotovic Jan 29 '14 at 14:06
  • Upvoted this because you need to set the Background image too. The problem here is that setting the BG to an empty image makes the background transparent, at least if it's a translucent tab bar. Which might not be what you wanted. – n13 Jul 30 '15 at 12:47
  • Null image doesn't work in iOS 11, at least up to and including beta 6. – lewis Aug 23 '17 at 13:39
  • 2
    Worked on iOS 12. But doesn't work on iOS13 anymore! Have anyone tried? There's a new `tabBar.standardAppearance.shadowImage` but it doesn't work at all... – SoftDesigner Sep 14 '19 at 08:30
  • this solution and all other solutions will affect the uitabbaritem title font, it will not apply the custom font if you have one. – JAHelia Nov 28 '19 at 08:34
40

Try setting a 1x1 pixel transparent shadow image for the UITabBar:

[[UITabBar appearance] setShadowImage:[UIImage imageNamed:@"transparentShadow.png"]];
Brian Liang
  • 7,734
  • 10
  • 57
  • 72
  • I know how to deal with UITabBarItem shadow and already removed it. My concern is the UITabBar shadow (the big line on the top) that many apps succeeded to remove. – httpete Jan 17 '13 at 02:55
  • It worked, thanks :) (Also for people wondering transparentShadow.png is a 1x1 transparent image) – httpete Jan 17 '13 at 03:05
  • Thanks mate.. A lots of tries are solved by your code.. Thanks again. – Manann Sseth Jan 16 '14 at 07:19
  • 4
    Important: The value for "setShadowImage" is by default ignored, unless you also set a background image with setBackgroundImage. So you always have to set both! [From Apple docs] – n13 Jul 30 '15 at 12:44
17

Swift 4

UITabBar.appearance().layer.borderWidth = 0.0
UITabBar.appearance().clipsToBounds = true
Ahmadreza
  • 6,950
  • 5
  • 50
  • 69
16

Swift

Try this for your custom tab bar. It will hide horizontal shadow line.

self.tabBar.setValue(true, forKey: "_hidesShadow")

Objective C

[self.tabBar setValue:@(YES) forKeyPath:@"_hidesShadow"];
Sourabh Sharma
  • 8,222
  • 5
  • 68
  • 78
  • 2
    You can also add this in Interface Builder in the Identity Inspector pane for the UITabBar under the section "User Defined Runtime Attributes". Hit the + to add a row and enter the _hidesShadow key and value. – Pat Niemeyer Jul 28 '16 at 05:25
  • 5
    This is accessing a private API, so could be cause for rejection and is likely to break in future versions of iOS. – Luke Rogers Feb 01 '17 at 12:06
  • 1
    I use the Swift code. It works for iOS 11 in 04,2018. – jiexishede Apr 24 '18 at 06:54
11

This code works both iOS 13 and below

if #available(iOS 13, *) {
    let appearance = self.tabBar.standardAppearance.copy()
    appearance.backgroundImage = UIImage()
    appearance.shadowImage = UIImage()
    appearance.shadowColor = .clear
    self.tabBar.standardAppearance = appearance
} else {
    self.tabBar.backgroundImage = UIImage()
    self.tabBar.shadowImage = UIImage()
}
Miravzal
  • 2,025
  • 1
  • 13
  • 11
2

Calling [[UITabBar appearance] setShadowImage:] will customise all UITabBar instances in your app.

If you want to customize just one UITTabBar, you can do this:

[self.tabBarController.navigationController.navigationBar setShadowImage:[UIImage new]];
Eric
  • 16,003
  • 15
  • 87
  • 139
2

Here's another easy to implement answer:

[self.tabBar setValue:@(YES) forKeyPath:@"_hidesShadow"];

Worked for me.

dbv
  • 677
  • 5
  • 5
  • 4
    Caveat is that this seems undocumented. Better use the - admittedly more tedious - methods above, setting shadow and background images. – n13 Jul 30 '15 at 12:45
2

Just be setting image it will not remove the shadow line you have to set it's borderWidth to 0. here is the code

[[UITabBar appearance] setShadowImage:[UIImage new]];

[UITabBar appearance].layer.borderWidth = 0.0f;

[UITabBar appearance].clipsToBounds = true;

1

Place this in your AppDelegate under didFinishLaunchingWithOptions:

[[UITabBar appearance] setShadowImage:[[UIImage alloc] init]];
[[UITabBar appearance] setBackgroundImage:[[UIImage alloc] init]];
DocAsh59
  • 400
  • 1
  • 6
  • 17
1

Try this, ** Objective-C **

//Remove shadow image by assigning nil value.
[[UITabBar appearance] setShadowImage: nil];

// or 

// Assing UIImage instance without image reference
[[UITabBar appearance] setShadowImage: [[UIImage alloc] init]];

** Swift **

//Remove shadow image by assigning nil value.
UITabBar.appearance().shadowImage = nil

// or 

// Assing UIImage instance without image reference
UITabBar.appearance().shadowImage = UIImage()


Here is apple guideline for shadowImage.

@available(iOS 6.0, *)
open var shadowImage: UIImage?

Default is nil. When non-nil, a custom shadow image to show instead of the default shadow image. For a custom shadow to be shown, a custom background image must also be set with -setBackgroundImage: (if the default background image is used, the default shadow image will be used).

Krunal
  • 77,632
  • 48
  • 245
  • 261
1

I have achieved the same look by following method.
1. Set the background bar tint colour to same as the main parent view background colour.
2.

this.TabBar.BarStyle = UIBarStyle.BlackOpaque;

I used it in Xamarin, Please verify the Swift syntax.

Shanu Singh
  • 365
  • 3
  • 16
1

if you need to remove the shadow line on iOS 13 from a tab bar that has a custom font, then you have to apply it this way:

if #available(iOS 13.0, *) {
   let appearance = UITabBarAppearance()
   appearance.stackedLayoutAppearance.normal.titleTextAttributes = ...
   appearance.stackedLayoutAppearance.selected.titleTextAttributes = ...
   appearance.shadowColor = .clear
   tabBar.standardAppearance = appearance
 }

JAHelia
  • 6,934
  • 17
  • 74
  • 134
0

In your view controller or view controllers or BasicViewController that most of the viewcontrollers inherit in the viewDidLoad just put these 2 lines:

[[UITabBar appearance] setBackgroundImage:[UIImage imageNamed:@"tab_bar_background"]];
[[UITabBar appearance] setShadowImage:[UIImage imageNamed:@"transparent_shadow"]];

Be sure transparent_shadow@2x.png is an image 1x1 or 2x2 transparent and the tab_bar_background@2x.png is an image 640x100 as the bottom bar is 50px in height.

Works on iOS 9.3

Catalin
  • 1,821
  • 4
  • 26
  • 32
0

Try this on viewDidload.

override func viewDidLoad() {
        super.viewDidLoad()

        self.tabBar.setValue(true, forKey: "_hidesShadow")
}

It work for me

anhtran
  • 21
  • 2
-2

In iOS 7 - this works:

[self.actionToolbar setShadowImage:[[UIImage alloc] init] forToolbarPosition:UIBarPositionAny];
[self.actionToolbar setBackgroundImage:[[UIImage alloc] init] forToolbarPosition:UIBarPositionAny barMetrics:UIBarMetricsDefault];

Hope that helps someone.

iOSProgrammingIsFun
  • 1,418
  • 1
  • 15
  • 32