How to draw a transparent UIToolbar or UINavigationBar in iOS7
Swift 3 (iOS 10)
Transparent UIToolbar
self.toolbar.setBackgroundImage(UIImage(),
forToolbarPosition: .any,
barMetrics: .default)
self.toolbar.setShadowImage(UIImage(), forToolbarPosition: .any)
Transparent UINavigationBar
self.navigationBar.setBackgroundImage(UIImage(), for: .default)
self.navigationBar.shadowImage = UIImage()
self.navigationBar.isTranslucent = true
Swift < 3
Transparent UIToolbar
self.toolbar.setBackgroundImage(UIImage(),
forToolbarPosition: UIBarPosition.Any,
barMetrics: UIBarMetrics.Default)
self.toolbar.setShadowImage(UIImage(),
forToolbarPosition: UIBarPosition.Any)
Transparent UINavigationBar
self.navigationBar.setBackgroundImage(UIImage(), forBarMetrics: UIBarMetrics.Default)
self.navigationBar.shadowImage = UIImage()
self.navigationBar.translucent = true
Objective-C
Transparent UIToolbar
[self.toolbar setBackgroundImage:[UIImage new]
forToolbarPosition:UIBarPositionAny
barMetrics:UIBarMetricsDefault];
[self.toolbar setShadowImage:[UIImage new]
forToolbarPosition:UIBarPositionAny];
Transparent UINavigationBar
[self.navigationBar setBackgroundImage:[UIImage new]
forBarMetrics:UIBarMetricsDefault];
self.navigationBar.shadowImage = [UIImage new];
self.navigationBar.translucent = YES;
Discussion
Setting translucent
to YES
on the navigation bar does the trick, due to a behavior discussed in the UINavigationBar
documentation. I'll report here the relevant fragment:
If you set this property to
YES
on a navigation bar with an opaque custom background image, the navigation bar will apply a system opacity less than 1.0 to the image.
Final result
How to make UIToolbar have a Clear Background?
[self.toolbar setBackgroundImage:[UIImage new]
forToolbarPosition:UIToolbarPositionAny
barMetrics:UIBarMetricsDefault];
[self.toolbar setBackgroundColor:[UIColor clearColor]];
Transparent UIToolbar
You can set the property translucent
to YES
and see if this helps.
How to make completely transparent navigation bar in iOS 7
From this answer
[self.navigationController.navigationBar setBackgroundImage:[UIImage new]
forBarMetrics:UIBarMetricsDefault];
self.navigationController.navigationBar.shadowImage = [UIImage new];
self.navigationController.navigationBar.translucent = YES;
self.navigationController.view.backgroundColor = [UIColor clearColor];
self.navigationController.navigationBar.backgroundColor = [UIColor clearColor];
Also, as suggested by Josh in the comments, to put the bar back to default:
[self.navigationController.navigationBar setBackgroundImage:nil
forBarMetrics:UIBarMetricsDefault];
How to make UIToolbar's background transparent?
If you want toolbar as
Transparent :
[toolBar setBackgroundImage:[[UIImage alloc] init] forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];
and if you want toolbar as Translucent :
[toolBar setBarStyle:UIBarStyleBlack];
toolBar.translucent = YES;
Hope it helps you.
How to set transparency of translucent navigation bar in iOS7?
It's late, but if anyone need the answer here it is.
If you want transparency in all navigationBar than write following lines in your AppDelegate's -(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
method:
## Objective C ##
[[UINavigationBar appearance] setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setShadowImage:[UIImage new]];
[[UINavigationBar appearance] setTranslucent:YES];
## Swift ##
UINavigationBar.appearance().setBackgroundImage(UIImage(), forBarMetrics: UIBarMetrics.Default)
UINavigationBar.appearance().shadowImage = UIImage()
UINavigationBar.appearance().translucent = true
For transparency in particular view, check this answer.
Related Topics
Why Does Apple Recommend to Use Dispatch_Once for Implementing the Singleton Pattern Under Arc
Scrollview Gesture Recognizer Eating All Touch Events
Capturing and Storing a Picture Taken with the Camera into a Local Database/Phonegap/Cordova/Ios
Uidatepicker Select Month and Year
Custom Init for Uiviewcontroller in Swift with Interface Setup in Storyboard
What Is "Self" Used for in Swift
Xcode Quits Unexpectedly Every Time I Open My Project
Custom Mkannotation Callout Bubble with Button
Linker Command Failed with Exit Code 1 (Use -V to See Invocation), Xcode 8, Swift 3
Invalid Swift Support/Invalid Implementation of Swift
Swiftui Navigationview Navigationbartitle Layoutconstraints Issue
How to Detect If a Video File Was Recorded in Portrait Orientation, or Landscape in iOS
Xcode 6.1 Missing Required Architecture X86_64 in File
How to Create Layout Constraints Programmatically
One Step Affine Transform for Rotation Around a Point
Objective C: Downloading File with Progress Bar
Is It Considered a Private API to Use App-Prefs:Root
Xcodebuild Different Provisioning Profile for Target Dependency