zoukankan      html  css  js  c++  java
  • 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]];

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

  • 相关阅读:
    首发Zend Studio 9.0.2正式版注册破解
    PHP 中Cookie和Session的使用
    PHP设计模式职责和单例模式
    软件工程工具学习(1)Visio
    java复习(4)数字处理类
    java复习(1)java与C++区别
    微信开发(1)微信防撤回功能实现
    java复习(3)字符串、数组
    java复习(2)java基础杂记
    java复习(5)接口、继承、多态
  • 原文地址:https://www.cnblogs.com/lihaibo-Leao/p/4248295.html
Copyright © 2011-2022 走看看