iOS设置导航栏透明度

As I support Colin‘s answer, I want to give you an additional hint to customize the appearance of an UINavigationBar including the 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.

Here I give you some code snippets:

  1. E.g. in your ..AppDelegate.m add these lines in didFinishLaunchingWithOptions
  2. //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.Implement convenience methods to programmatically creates UIImage objects, e.g. create a new category for UIImage:

  3. //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.Re-work your image creation in 1. (#import "UIImage+initWithColor.h" in AppDelegate.m and replace the "nil"s):

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

    // 用图片当作背景 设置透明度

时间: 2024-11-05 21:36:58

iOS设置导航栏透明度的相关文章

iOS 设置导航栏之二(设置导航栏的颜色、文字的颜色、左边按钮的文字及颜色)

                  #import <UIKit/UIKit.h> @interface AppDelegate : UIResponder <UIApplicationDelegate> @property (strong, nonatomic) UIWindow *window; @end #import "AppDelegate.h" #import "KeyViewController.h" @interface Ap

iOS 设置导航栏的颜色和导航栏上文字的颜色

#import <UIKit/UIKit.h> @interface AppDelegate : UIResponder <UIApplicationDelegate> @property (strong, nonatomic) UIWindow *window; @end #import "AppDelegate.h" #import "KeyViewController.h" @interface AppDelegate () @end

iOS设置导航栏样式(UINavigationController)

//设置导航栏baritem和返回baiitem样式 UIBarButtonItem *barItem = [UIBarButtonItem appearance]; //去掉返回按钮上的字 [barItem setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -60) forBarMetrics:UIBarMetricsDefault]; //把按钮设置为白色 barItem.tintColor = [UIColor whiteColor]

iOS - 设置导航栏之标题栏居中、标题栏的背景颜色

本章实现效果: Untitled.gif 前言: 项目中很多需求是要求自定义标题栏居中的,本人最近就遇到这中需求,如果用系统自带的titleView设置的话,不会居中,经过尝试,发现titleview的起点位置和尺寸依赖于leftBarButtonItem和rightBarButtonItem的位置.下面给出我的解决方案 首先自定义一个标题View #import <UIKit/UIKit.h> @interface CustomTitleView : UIView @property (no

IOS设置导航栏字体大小及颜色

方法一: 自定义视图,定义一个lable,相关属性在lable里设置 核心方法: self.navigationItem.titleView = titleLabel; 方法二:用系统方法直接设置 [self.navigationController.navigationBar setTitleTextAttributes:@{ NSFontAttributeName:[UIFont systemFontOfSize:20], NSForegroundColorAttributeName:Whi

ios UINavigationController 导航栏

1.关于导航栏左右两边的按钮 1.隐藏导航栏上的返回字体 //Swift UIBarButtonItem.appearance().setBackButtonTitlePositionAdjustment(UIOffsetMake(0, -60), forBarMetrics: .Default) //OC [[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -60) forBarM

iOS不得姐项目--appearance的妙用,再一次设置导航栏返回按钮,导航栏左右按钮的封装(巧用分类)

一.UI_APPEARANCE_SELECTOR 彩票项目中appearance的用法一直没有搞明白,这次通过第二个项目中老师的讲解,更深一层次的了解到了很多关于appearance的作用以及使用方法. 在iOS属性后有UI_APPEARANCE_SELECTOR标志都可以一次性统一设置.这种情况还有很多.比如说统一设置UITabbarItem的文字颜色 通过appearance来同意设置属性最好是在+ (void)initialize;方法里面. 项目中设置导航栏背景图片的代码: 项目中设置T

【转】iOS中设置导航栏标题的字体颜色和大小

原文网址:http://www.360doc.com/content/15/0417/11/20919452_463847404.shtml iOS中设置导航栏标题的字体颜色和大小,有需要的朋友可以参考下. 在平时开发项目的时候,难免会遇到修改导航栏字体大小和颜色的需求,一般使用自定义视图的方法,其实还存在一种方法. 方法一:(自定义视图的方法,一般人也会采用这样的方式) 就是在导航向上添加一个titleView,可以使用一个label,再设置label的背景颜色透明,字体什么的设置就很简单了.

iOS项目开发实战——自定义设置导航栏和状态栏背景

在iPhone的状态栏顶部,会有很多的信息,如运营商,信号强弱,网络状况,时间,电量等等.我们使用很多的App,如微信,QQ等等,都会发现导航栏的背景或者说图片都是重新设置过的,这样就能增加用户体验,也能更加节省下方的屏幕空间.那如何来设置导航栏的图片呢? (1)首先建立一个iOS项目,语言选择Swift.并且在Images.xcassets中拖入一张图片,大小要自己设置好,用来作为顶部导航栏的背景. (2)在Main.storyboard中选中ViewController,然后选择Editor