code-blind
code-blind

Reputation: 169

How to remove or hide the toolbars' top shadow

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

Upvotes: 6

Views: 5734

Answers (6)

Ankit Bhardwaj
Ankit Bhardwaj

Reputation: 221

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+

Upvotes: 5

reinaldoluckman
reinaldoluckman

Reputation: 6348

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

Upvotes: 8

PricklyApps
PricklyApps

Reputation: 113

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.

Upvotes: 0

natbro
natbro

Reputation: 1038

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];
}

Upvotes: 15

PonnuruKumar
PonnuruKumar

Reputation: 9

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

Upvotes: -1

Paras Joshi
Paras Joshi

Reputation: 20541

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...

Upvotes: 2

Related Questions