博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
iOS设置导航栏透明度
阅读量:7106 次
发布时间:2019-06-28

本文共 2494 字,大约阅读时间需要 8 分钟。

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 barUIImage *gradientImage44 = nil; //replace "nil" with your method to programmatically create a UIImage object with transparent colors for portrait orientationUIImage *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 
    @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
    @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]];

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

转载地址:http://japhl.baihongyu.com/

你可能感兴趣的文章
第四课-第四讲04_04_grep及正则表达式
查看>>
Runtime Method Swizzling开发实例汇总
查看>>
4.22 磁盘限额
查看>>
录音文件转文字,有了这个工具,再也不用担心记不上笔记了
查看>>
ubuntu下helloworld
查看>>
什么是区块链
查看>>
MyEclipse 2014 加速启动设置
查看>>
UI设计师都关注的字体设计技巧
查看>>
Gdtool为您解答微信公众号手机无法直接下载APK文件是怎么回事
查看>>
apache下mod_jk模块学习随笔
查看>>
Android学习--03-活动
查看>>
VMware-workstation-full-9.0.0-812388附注册机
查看>>
重新安装.net framework 4.5
查看>>
常用开发资源收集
查看>>
年近30,朋友聚会都聊什么?
查看>>
发布jar包至maven本地库及私服
查看>>
Xshell 创建RSA登录Linux 主机
查看>>
非常酷的国外网站导航设计案例欣赏
查看>>
ASP.NET身份验证的探讨
查看>>
Java反射与注解
查看>>