更改导航栏的 alpha 值 [英] Change the alpha value of the navigation bar

查看:33
本文介绍了更改导航栏的 alpha 值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能吗?

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

I want to change the alpha value of the navigation bar in my view controller (in an animation), but if I do self.navigationController.navigationBar.alpha = 0.0;, the portion of the screen the navigationBar took up totally disappears and leaves a black box, which is not what I'd like (I'd prefer it to be the color of self.view's background).

推荐答案

因为我支持 Colin 的回答,所以我想给你一个额外的提示来自定义 UINavigationBar 的外观,包括 alpha.

As I support Colin's answer, I want to give you an additional hint to customize the appearance of an UINavigationBar including the alpha.

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

The trick is to use UIAppearance for your NavigationBar. This enables you to assign an UIImage to your NavigationBar's backgroundImage. You can generate these UIImages programmatically and use for that UIColors and set the colors' alpha properties as you want. I've done this in one of my own applications and it works as expected.

这里我给你一些代码片段:

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

  1. E.g. in your ..AppDelegate.m add these lines in 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];

  • 实现以编程方式创建 UIImage 对象的便捷方法,例如为 UIImage 创建一个新类别:

  • Implement convenience methods to programmatically creates UIImage objects, e.g. create a new category for UIImage:

    //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;
    }
    

  • 在 1 中重新创建图像.(在 AppDelegate.m 中#import "UIImage+initWithColor.h" 并替换 "nil"):

  • Re-work your image creation in 1. (#import "UIImage+initWithColor.h" in AppDelegate.m and replace the "nil"s):

    这是您感兴趣的地方:通过更改颜色的 alpha 属性,您也会影响导航栏的不透明度级别!

                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]];
    

    我创建了一个小型演示项目,并向您添加了两个屏幕截图:视图本身具有黄色背景色.NavigationBar 的 backgroundImages 具有红色.屏幕截图 1 显示了一个值为 alpha = 0.2 的 NavigationBar.屏幕截图 2 显示了一个值为 alpha = 0.8 的 NavigationBar.

    I created a small demo project and add you two screenshots: the view itself has a yellow backgroundColor. The backgroundImages of the NavigationBar have a red color. Screenshot 1 shows a NavigationBar with a value for alpha = 0.2. Screenshot 2 shows a NavigationBar with a value for alpha = 0.8.

    这篇关于更改导航栏的 alpha 值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆