如何使iOS 7中完全透明的导航栏
我想我的应用程序中的UINavigationBar是完全透明的,并直接在它下面的viewcontroller。 但是,我能find的唯一代码使它变得透明而不透明。 我知道这可以在iOS 7中完成,因为它在笔记应用程序中使用。 我的问题是,他们用来做什么的代码?
从这个答案
[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];
另外,正如Josh在评论中所build议的那样,要将条形恢复为默认值:
[self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
对于Swift3和Swift4
self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default) self.navigationController?.navigationBar.shadowImage = UIImage() self.navigationController?.navigationBar.isTranslucent = true
对于Swift2.2
self.navigationController?.navigationBar.setBackgroundImage(UIImage(), forBarMetrics: .Default) self.navigationController?.navigationBar.shadowImage = UIImage() self.navigationController?.navigationBar.translucent = true
对于Objective-C
[self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault]; self.navigationController.navigationBar.shadowImage = [UIImage new]; self.navigationController.navigationBar.translucent = YES;
作为Objective-C的自包含解决scheme类别:
UINavigationController的+ TransparentNavigationController.h
@interface UINavigationController (TransparentNavigationController) - (void)presentTransparentNavigationBar; - (void)hideTransparentNavigationBar; @end
UINavigationController的+ TransparentNavigationController.m
#import "UINavigationController+TransparentNavigationController.h" @implementation UINavigationController (TransparentNavigationController) - (void)presentTransparentNavigationBar { [self.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault]; [self.navigationBar setTranslucent:YES]; [self.navigationBar setShadowImage:[UIImage new]]; [self setNavigationBarHidden:NO animated:YES]; } - (void)hideTransparentNavigationBar { [self setNavigationBarHidden:YES animated:NO]; [self.navigationBar setBackgroundImage:[[UINavigationBar appearance] backgroundImageForBarMetrics:UIBarMetricsDefault] forBarMetrics:UIBarMetricsDefault]; [self.navigationBar setTranslucent:[[UINavigationBar appearance] isTranslucent]]; [self.navigationBar setShadowImage:[[UINavigationBar appearance] shadowImage]]; } @end
现在,您可以在UIViewController
导入类别,并调用导航控制器上的方法 – 例如:
#import "UINavigationController+TransparentNavigationController.h" - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; [self.navigationController presentTransparentNavigationBar]; } - (void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; [self.navigationController hideTransparentNavigationBar]; }
在Swift中有一个类似的解决scheme:
import Foundation import UIKit extension UINavigationController { public func presentTransparentNavigationBar() { navigationBar.setBackgroundImage(UIImage(), forBarMetrics:UIBarMetrics.Default) navigationBar.translucent = true navigationBar.shadowImage = UIImage() setNavigationBarHidden(false, animated:true) } public func hideTransparentNavigationBar() { setNavigationBarHidden(true, animated:false) navigationBar.setBackgroundImage(UINavigationBar.appearance().backgroundImageForBarMetrics(UIBarMetrics.Default), forBarMetrics:UIBarMetrics.Default) navigationBar.translucent = UINavigationBar.appearance().translucent navigationBar.shadowImage = UINavigationBar.appearance().shadowImage } }
艾伦忘了一行
self.navigationController.navigationBar.backgroundColor = [UIColor clearColor];
所以我有:
[self.navigationController.navigationBar setTranslucent:YES]; self.navigationController.view.backgroundColor = [UIColor clearColor]; [self.navigationController.navigationBar setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault]; self.navigationController.navigationBar.shadowImage = [[UIImage alloc] init]; self.navigationController.navigationBar.backgroundColor = [UIColor clearColor];
@ Zorayr的伟大答案修改为Swift 3:
import Foundation import UIKit extension UINavigationController { public func presentTransparentNavigationBar() { navigationBar.setBackgroundImage(UIImage(), for:.default) navigationBar.isTranslucent = true navigationBar.shadowImage = UIImage() setNavigationBarHidden(false, animated:true) } public func hideTransparentNavigationBar() { setNavigationBarHidden(true, animated:false) navigationBar.setBackgroundImage(UINavigationBar.appearance().backgroundImage(for: UIBarMetrics.default), for:.default) navigationBar.isTranslucent = UINavigationBar.appearance().isTranslucent navigationBar.shadowImage = UINavigationBar.appearance().shadowImage } }
使用UINavigationBar + Addition pod,然后只需调用:
UINavigationBar *navigationBar = self.navigationController.navigationBar; [navigationBar makeTransparent];
[(UIView *)[self.navigationController.navigationBar.subviews objectAtIndex:0] setAlpha:0.0f];
那条线似乎对我来说是完美的