更改导航栏的Alpha值

这可能吗?

我想改变我的视图控制器(在animation中)的导航栏的alpha值,但如果我做self.navigationController.navigationBar.alpha = 0.0; ,导航条所占的屏幕部分完全消失,并留下一个黑框,这不是我想要的(我更喜欢它是self.view的背景颜色)。

正如我支持科林的回答,我想给你一个额外的提示,以定制包括阿尔法在内的UINavigationBar的外观。

诀窍是使用UIAppearance为您的NavigationBar。 这使您可以将UIImage分配给NavigationBar的backgroundImage 。 您可以编程方式生成这些UIImages,并使用该UIColors并根据需要设置颜色的alpha属性 。 我已经在我自己的一个应用程序中完成了这个工作,并且按预期工作。

在这里,我给你一些代码片段

  1. 例如在你的..AppDelegate.m在didFinishLaunchingWithOptions中添加这些行:

     //create background images for the navigation bar UIImage *gradientImage44 = nil; //replace "nil" with your method to programmatically create a UIImage object with transparent colors for portrait orientation UIImage *gradientImage32 = nil; //replace "nil" with your method to programmatically create a UIImage object with transparent colors for landscape orientation //customize the appearance of UINavigationBar [[UINavigationBar appearance] setBackgroundImage:gradientImage44 forBarMetrics:UIBarMetricsDefault]; [[UINavigationBar appearance] setBackgroundImage:gradientImage32 forBarMetrics:UIBarMetricsLandscapePhone]; [[UINavigationBar appearance] setBarStyle:UIBarStyleDefault]; 
  2. 实现便捷方法以编程方式创buildUIImage对象,例如为UIImage创build一个新类别:

     //UIImage+initWithColor.h // #import <UIKit/UIKit.h> @interface UIImage (initWithColor) //programmatically create an UIImage with 1 pixel of a given color + (UIImage *)imageWithColor:(UIColor *)color; //implement additional methods here to create images with gradients etc. //[..] @end //UIImage+initWithColor.m // #import "UIImage+initWithColor.h" #import <QuartzCore/QuartzCore.h> @implementation UIImage (initWithColor) + (UIImage *)imageWithColor:(UIColor *)color { CGRect rect = CGRectMake(0, 0, 1, 1); // create a 1 by 1 pixel context UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0); [color setFill]; UIRectFill(rect); UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return image; } 
  3. 重新在1中创build图像创build(在AppDelegate.m中#import“UIImage + initWithColor.h”并replace“nil”):

这是你感兴趣的地方:通过改变你的颜色的alpha属性,你也影响了你的NavigationBar的不透明度!

  UIImage *gradientImage44 = [UIImage imageWithColor:[UIColor colorWithRed:1.0 green:0.0 blue:1.0 alpha:0.2]]; UIImage *gradientImage32 = [UIImage imageWithColor:[UIColor colorWithRed:1.0 green:0.0 blue:1.0 alpha:0.2]]; 

我创build了一个小的演示项目,并添加了两个截图 :视图本身有一个黄色的backgroundColor。 NavigationBar的backgroundImages具有红色。 屏幕截图1显示了一个导航条,其alpha = 0.2值。 屏幕截图2显示了一个导航条,其值为alpha = 0.8。

NavigationBar alpha = 0.2的屏幕截图NavigationBar的屏幕截图,alpha = 0.8

这样做的最直接的方法是修改navigationBar背景视图的alpha组件,而此时(iOS9)是第一个navigationBar子视图。 但是请注意,我们不知道在以后的版本中子视图层次是否会被苹果更改,所以要小心。

 let navigationBackgroundView = self.navigationController?.navigationBar.subviews.first navigationBackgroundView?.alpha = 0.7 

直接从Apple Developer参考:

“只有less量的直接定制可以在导航栏中进行,具体来说,可以修改barStyletintColorbarStyle属性,但是绝对不能直接改变UIView level属性,比如frameboundsalpha ,或直接hidden属性。“

但是,您可以设置导航栏的半透明属性。 如果你这样做[self.navigationController.navigationBar setTranslucent:YES]; 应该解决你的问题。 你也可以尝试看看是否有任何UIBarStyle枚举是你想要的东西。

MickBraun在Swift的回答:

  1. 在AppDelegate.swift中,在didFinishLaunchingWithOptions中添加这些行:

     // create background images for the navigation bar let gradientImage44 = UIImage.imageWithColor(UIColor(red: 1.0, green: 0.0, blue: 1.0, alpha: 0.2)) let gradientImage32 = UIImage.imageWithColor(UIColor(red: 1.0, green: 0.0, blue: 1.0, alpha: 0.2)) // customize the appearance of UINavigationBar UINavigationBar.appearance().setBackgroundImage(gradientImage44, forBarMetrics: .Default) UINavigationBar.appearance().setBackgroundImage(gradientImage32, forBarMetrics: .Compact) UINavigationBar.appearance().barStyle = .Default 
  2. 实现便捷方法以编程方式创buildUIImage对象。

     class func imageWithColor(colour: UIColor) -> UIImage { let rect = CGRectMake(0, 0, 1, 1) // Create a 1 by 1 pixel content UIGraphicsBeginImageContextWithOptions(rect.size, false, 0.0) colour.setFill() UIRectFill(rect) let image = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() return image } 

如果你只是想animation的方式摆脱navigationBar栏,你可以这样做:

 [self.navigationController setNavigationBarHidden:YES animated:YES]; 

如果你想控制animation,并将其设置为0.0 ,那么阅读:

你所看到的“黑匣子”是来自底层的视图或窗口。 如果你只是想让你的视图颜色,而不是“黑匣子”做:

 self.navigationController.view.backgroundColor = self.view.backgroundColor; [UIView animateWithDuration:1.0 delay:1.0 options:0 animations:^{ self.navigationController.navigationBar.alpha = 0.0; } completion:NULL]; 

如果你想让你的实际视图成为navigationBar位置,你需要增加视图的height

 [UIView animateWithDuration:1.0 delay:1.0 options:0 animations:^{ self.navigationController.navigationBar.alpha = 0.0; CGFloat navigationBarHeight = self.navigationController.navigationBar.frame.size.height; CGRect frame = self.view.frame; frame.origin.y -= navigationBarHeight; frame.size.height += navigationBarHeight; self.view.frame = frame; } completion:NULL]; 

你有几个选项,部分取决于你的UINavigationBar的barStyle。 主要的是意识到你可能不一定要animation的alpha属性来获得你所描述的效果。

UIBarStyleDefaultUIBarStyleBlackOpaque选项A是将您的UINavigationBar半透明属性设置为YES,然后animationalpha:

  navigationBar.translucent = YES; // ideally set this early, in the nib/storyboard, or viewDidLoad ... [UIView animateWithDuration: 1.0 animations: ^{ // toggle: navigationBar.alpha = navigationBar.alpha == 0 ? 1.0 : 0.0; }]; 

在这种情况下,你的视图将被定位在你的导航栏后面,即使它的alpha是1.0。 这种情况下的缺点是,即使使用1.0 alpha,您可能会在UINavigationBar后面看到一些颜色。 此外,所有的子视图都需要从顶部向下44点。

UIBarStyleDefaultUIBarStyleBlackOpaque选项B是在交叉过渡animation中隐藏导航栏。 这将暴露UINavigationBar的超级视图。 如果您使用的是UINavigationController那么您将看到UINavigationController视图的黑色背景 – 但是您可以设置UINavigationController视图的背景颜色以匹配您的视图以获得所需的效果:

  UINavigationBar* navigationBar = self.navigationController.navigationBar; self.navigationController.view.backgroundColor = self.view.backgroundColor; [UIView transitionWithView: navigationBar duration: 1.0 options: UIViewAnimationOptionTransitionCrossDissolve animations: ^{ // toggle: navigationBar.hidden = !navigationBar.hidden; } completion: nil]; 

有一点要注意这个解决scheme可能是一个布局问题,如果UINavigationController更新你的视图框架,因为你隐藏的UINavigationBar 。 这样可以很好,只是如果你的子视图锚定在左上angular,你的子视图可能会上移44个像素。 要解决这个问题,你可能会考虑将你的子视图锚定到你的视图的底部(或者用弹簧或者布局约束)。

UIBarStyleDefaultUIBarStyleBlackOpaque选项C是用另一个视图覆盖UINavigationBar ,同样使用交叉过渡animation:

  UINavigationBar* navigationBar = self.navigationController.navigationBar; [UIView transitionWithView: navigationBar duration: 1.0 options: UIViewAnimationOptionTransitionCrossDissolve | UIViewAnimationOptionAllowAnimatedContent animations: ^{ // toggle: const int tag = 1111; UIView* navOverlayView = [navigationBar viewWithTag: tag]; if ( navOverlayView == nil ) { navOverlayView = [[UIView alloc] initWithFrame: CGRectInset( navigationBar.bounds, 0, -3 ) ]; navOverlayView.backgroundColor = self.view.backgroundColor; navOverlayView.tag = tag; [navigationBar addSubview: navOverlayView]; } else { [navOverlayView removeFromSuperview]; } } completion: nil]; 

UIBarStyleBlackTranslucent :这个选项是最简单的,因为UINavigationBar已经是半透明的,你的视图已经在它后面了。 简单地animation阿尔法:

  [UIView animateWithDuration: 1.0 animations: ^{ // toggle: navigationBar.alpha = navigationBar.alpha == 0 ? 1.0 : 0.0; }]; 

这应该得到你想要的效果:

 self.navigationController.navigationBar.alpha = 0.0; self.navigationController.navigationBar.frame = CGRectMake(0,0,320,0); 

没有尝试过这个animation,在我的viewDidAppear尽pipe工作,希望它的作品。

您还可以尝试在导航栏下方设置背景视图并直接修改此视图。

 private lazy var backgroundView: UIView = UIView() ... private func setupNavigationBarBackground() { guard let navigationBar = navigationController?.navigationBar else { return } navigationBar.setBackgroundImage(UIImage(), for: .default) navigationBar.isTranslucent = true view.addSubview(backgroundView) backgroundView.alpha = 0 backgroundView.backgroundColor = .red backgroundView.translatesAutoresizingMaskIntoConstraints = false backgroundView.topAnchor.constraint(equalTo: topLayoutGuide.topAnchor).isActive = true backgroundView.bottomAnchor.constraint(equalTo: topLayoutGuide.bottomAnchor).isActive = true backgroundView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true backgroundView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true } private func changeNavigationBarAlpha(to alpha: CGFloat) { backgroundView.alpha = alpha } 

Swift3

 var navAlpha = // Your appropriate calculation self.navigationController!.navigationBar.backgroundColor = UIColor.red.withAlphaComponent(navAlpha)