6

After removing the background of the toolbar, with an image mask, a shadow line still remains above the toolbar. How do we get rid of it? As you can see, by the image below, I want to use the toolbar and buttons but no background or top shadow.

const float colorMask[6] = {222, 255, 222, 255, 222, 255};
UIImage *_img = [[UIImage alloc] init];
UIImage *_maskedImage = [UIImage imageWithCGImage:CGImageCreateWithMaskingColors(_img.CGImage, colorMask)];
[self.navigationController.toolbar setBackgroundImage:_maskedImage forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];

Toolbar top shadow after hiding background with image mask

code-blind
  • 169
  • 1
  • 4
  • 13
  • Don't edit your question into an answer, it doesn't make sense any more. Answering your own question is fine (you can accept your own answer, as well). – jrturton Dec 24 '12 at 08:06
  • @jrturton Oh, okay thank you. I hesitated to use "Answer my own Question"; wasn't sure. `setShadowImage` property for `toolbar` evaded me because it's under `UIToolbar`; in the developer documentation. – code-blind Dec 24 '12 at 08:15

6 Answers6

15

None of the other answers worked on iOS7, some didn't seem to work consistently on older iOS versions either. This (paraphrasing http://samplecodebank.blogspot.com/2013/06/UIToolbar-setShadowImage-forToolbarPosition-example.html) works consistently on 5.1+ for me and is concise and more performant than generating custom background images and color masks.

toolbar.backgroundColor = [UIColor clearColor];
if ([toolbar respondsToSelector:@selector(setBackgroundImage:forToolbarPosition:barMetrics:)]) {
  [toolbar setBackgroundImage:[UIImage new] forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];
}
if ([toolbar respondsToSelector:@selector(setShadowImage:forToolbarPosition:)]) {
  [toolbar setShadowImage:[UIImage new] forToolbarPosition:UIToolbarPositionAny];
}
natbro
  • 1,038
  • 12
  • 13
8

On iOS 7, set [toolBar setClipsToBounds:YES].

reinaldoluckman
  • 6,317
  • 8
  • 42
  • 50
5

Add this line also

[toolbar setShadowImage:_maskedImage forToolbarPosition:UIToolbarPositionAny];

Two important notes:

  1. You must also set the background image as well, otherwise this won't do anything.
  2. This is for iOS 6+
iwasrobbed
  • 46,496
  • 21
  • 150
  • 195
Ankit Bhardwaj
  • 221
  • 2
  • 9
  • Can you correct your code format, so it appears in a code sample area. Makes easier to read. – Popeye Dec 24 '12 at 11:21
2

First Add QuartzCore/QuartzCore framework in your project and after import it this in your .m file like bellow...

#import <QuartzCore/QuartzCore.h>

and after just add this bellow code...

    yourToolBar.layer.shadowOffset = CGSizeMake(0.0f, 0.0f);

    yourToolBar.layer.shadowOpacity =0.0f;

    yourToolBar.layer.shadowRadius = 0.0f;

hope this helpful to you...

Paras Joshi
  • 20,427
  • 11
  • 57
  • 70
0

None of the other answers worked for me in iOS 7, so here is what I did using Interface Builder:

  1. Add the toolbar to a UIView.
  2. Size the UIView the same as the UIToolbar.
  3. Drag the top of the UIView down until it just covers the top of the UIToolbar.
  4. Using the Attribute Inspector click the 'Clip Subviews'.

Doing this will clip the top of the toolbar off and thus remove the gray shadow.

splrs
  • 2,424
  • 2
  • 19
  • 29
PricklyApps
  • 113
  • 6
-1

setBackgroundImage:_maskedImage need to remove the shadow or else call the customized shadow clear method after toolbar hide