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:
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];
2.Implement convenience methods to programmatically creates UIImage objects, e.g. create a new category for UIImage:
- //create background images for the navigation bar
- //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(, , , );
- // create a 1 by 1 pixel context
- UIGraphicsBeginImageContextWithOptions(rect.size, NO, );
- [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):
- //UIImage+initWithColor.h
- 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]];
// 用图片当作背景 设置透明度
- UIImage *gradientImage44 = [UIImage imageWithColor:[UIColor colorWithRed:1.0 green:0.0 blue:1.0 alpha:0.2]];
iOS设置导航栏透明度的更多相关文章
- IOS 设置导航栏
//设置导航栏的标题 self.navigationItem setTitle:@"我的标题"; //设置导航条标题属性:字体大小/字体颜色…… /*设置头的属性:setTitle ...
- IOS 设置导航栏全局样式
// 1.设置导航栏背景 UINavigationBar *bar = [UINavigationBar appearance]; [bar setBackgroundImage:[UIImage r ...
- iOS 设置导航栏之二(设置导航栏的颜色、文字的颜色、左边按钮的文字及颜色)
#import <UIKit/UIKit.h> @interface AppDelegate : UIResponder <UIApplicati ...
- iOS 设置导航栏的颜色和导航栏上文字的颜色
#import <UIKit/UIKit.h> @interface AppDelegate : UIResponder <UIApplicationDelegate> @pr ...
- iOS设置导航栏样式(UINavigationController)
//设置导航栏baritem和返回baiitem样式 UIBarButtonItem *barItem = [UIBarButtonItem appearance]; //去掉返回按钮上的字 [bar ...
- iOS 设置导航栏全透明
- (void)viewWillAppear:(BOOL)animated{ //设置导航栏背景图片为一个空的image,这样就透明了 [self.navigationController.navig ...
- ios 设置导航栏背景色
//设置导航栏背景色 如果上面的不好用 就用下面的 [self.navigationController.navigationBar setBackgroundImage:[UIImage image ...
- IOS设置导航栏字体大小及颜色
方法一: 自定义视图,定义一个lable,相关属性在lable里设置 核心方法: self.navigationItem.titleView = titleLabel; 方法二:用系统方法直接设置 [ ...
- iOS设置导航栏标题
方法一:在UIViewController中设置self.title. 方法二:设置self.navigationItem.titleView.
随机推荐
- Postman---html中get和post的区别和使用
get和post的区别和使用 Html中post和get区别,是不是用get的方法用post都能办到? Http定义了与服务器交互的不同方法,最基本的方法有4种,分别是GET,POST,PUT,DEL ...
- python3 JSON对象的学习
import jsonname = { 'alex':[22,'M'], 'tony':[21,'F'],} name_after = json.dumps(name)name_after = nam ...
- C#代码覆盖率 -vsinstr和OpenCover
最近接触的项目涉及到C#开发的应用,测试过程中我们需要去分析C#的代码覆盖率,问了一些人,在网上也搜了一些,零碎的找到很多资料,但是都不是很完整,实际使用的过程中还是走了不少弯路.到现在为止,有两种可 ...
- 并且需要用websocket实时接收数据 VS 组件ng2websocket的
chart.service.ts: import { Injectable } from '@angular/core'; import { WebSocketService } from './we ...
- 181213 - 解决Android的应用APP背景色突然被改变的问题
在魅族最新的特定版本出现APP背景突然被改变颜色的问题 出问题的机型相关信息 型号:魅族16th Plus Android 版本: 8.1.0 安全补丁 版本: 2018年10月1日 Flyme 版本 ...
- mapper.xml文件
1. 概述 mybatis的真正强大在于它的映射语句.由于它的异常强大,映射器的XML文件就显得相对简单,如果拿它跟具有相同功能的JDBC代码进行对比,省掉将近95%的代码.mybatis是针对SQL ...
- ToStringBuilder学习(一):常用方法介绍
一.简介与引入 1.ToStringBuilder.HashCodeBuilder.EqualsBuilder.ToStringStyle.ReflectionToStringBuilder.C ...
- Mybatis设置超时时间
Mybatis设置超时时间 mybatis如果不指定,默认超时时间是不做限制的,默认值为0.mybatis sql配置超时时间有两种方法: 1.全局配置 在mybatis配置文件的settings节点 ...
- 记centos 安装 mysql5.7.23.tar.gz教程
1.下载tar包,这里使用wget从官网下载 wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.22-linux-glibc2. ...
- java模拟http请求上传文件,基于Apache的httpclient
1.依赖 模拟http端的请求需要依赖Apache的httpclient,需要第三方JSON支持,项目中添加 <dependency> <groupId>org.apache& ...