How to remove or hide the toolbars' top shadow
Asked Answered
C

6

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

Costermonger answered 24/12, 2012 at 7:6 Comment(2)
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).Outrage
@Outrage 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.Costermonger
T
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+
Tartlet answered 24/12, 2012 at 7:50 Comment(1)
Can you correct your code format, so it appears in a code sample area. Makes easier to read.Elevenses
D
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];
}
Diet answered 10/11, 2013 at 18:47 Comment(0)
F
8

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

Flagellant answered 14/1, 2014 at 14:40 Comment(0)
T
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+
Tartlet answered 24/12, 2012 at 7:50 Comment(1)
Can you correct your code format, so it appears in a code sample area. Makes easier to read.Elevenses
C
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...

Crites answered 24/12, 2012 at 7:14 Comment(1)
this has no impact on iOS7 that i can tell.Diet
E
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.

Esprit answered 30/12, 2013 at 0:22 Comment(0)
S
-1

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

Solberg answered 24/12, 2012 at 7:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.