Tuesday, June 11, 2013

UIToolbar setShadowImage example in Objective C (iOS).

UIToolbar setShadowImage

Sets the image to use for the toolbar shadow in a given position.

- (void)setShadowImage:(UIImage *)shadowImage forToolbarPosition:(UIToolbarPosition)topOrBottom

Parameters of [UIToolbar setShadowImage]
shadowImage
The image to use for the toolbar shadow in the position specified by topOrBottom.
topOrBottom
A toolbar position constant. You can use this parameter to indicate whether the shadowImage is intended for a toolbar at the top or bottom of the view.

Discussion of [UIToolbar setShadowImage]
When the shadowImage parameter is nil, the default shadow will be used. When non-nil, the shadowImage property is a custom shadow image to show instead of the default. Using the topOrBottom parameter, you can set a different shadow for toolbars at the top and bottom of the view. For a custom shadow image to be shown, a custom background image must also be set with the setBackgroundImage:forToolbarPosition:barMetrics: method. If the default background image is used, then the default shadow image will be used regardless of the value of the shadowImage parameter.

UIToolbar setShadowImage example.
Add this line also

[toolbar setShadowImage:_maskedImage forToolbarPosition:UIToolbarPositionAny];
But beware this will only work in iOS6

Example of [UIToolbar setShadowImage].
- (void)setTransparent
{
    //iOS3+
    self.backgroundColor = [UIColor clearColor];

    //iOS5+
    if ([self respondsToSelector:@selector(setBackgroundImage:forToolbarPosition:barMetrics:)])
    {
        [self setBackgroundImage:[[UIImage new] autorelease] forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];
    }
    //iOS6+
    if ([self respondsToSelector:@selector(setShadowImage:forToolbarPosition:)])
    {
        [self setShadowImage:[[UIImage new] autorelease] forToolbarPosition:UIToolbarPositionAny];
    }
}

End of UIToolbar setShadowImage example article.