Transparent UIToolbar

前端 未结 7 954
鱼传尺愫
鱼传尺愫 2021-02-14 19:29

I wrote the following code to make my toolbar transparent.

[mtoolbar setBackgroundColor:[UIColor clearColor]];

How do I make UIToolbar

7条回答
  •  心在旅途
    2021-02-14 19:56

    The following works in iOS 5 (and iOS 6 beta 4, although a slight top shadow is still visible there).

    Please note: Making a UIToolbar or UINavigationBar transparent is rarely a good idea, and modifying Apple's UIKit elements in such a way is bound to break sooner or later.

    TransparentToolbar.h

    #import 
    
    @interface TransparentToolbar : UIToolbar
    
    @end
    

    TransparentToolbar.m

    #import "TransparentToolbar.h"
    
    @implementation TransparentToolbar
    
    -(void)insertSubview:(UIView *)view atIndex:(NSInteger)index
    {
        //  This method is called with a view of class "UINavigationBarBackground" or "_UIToolbarBackground", respectively. It would be possible to check for this with NSStringFromClass([view class]) to be completely sure that we're skipping the right view.
    
        if (index != 0)
        {
            [super insertSubview:view atIndex:index];
        }
        else
        {
            // insert your custom background view, if you want to
        }
    }
    
    
    @end
    

    EDIT: In iOS 5+, it's also possible to simply set the backgroundImage (which could be transparent). This is certainly the "cleaner" solution, but is less flexible than a custom UIView.

    [someToolbar setBackgroundImage:[UIImage imageNamed:@"clear"] forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];
    

提交回复
热议问题