【问题标题】:UINavigationBar with image and default gradient background with iOS5UINavigationBar 与 iOS5 的图像和默认渐变背景
【发布时间】:2011-12-20 18:21:44
【问题描述】:

我正在尝试使用 iOS5 中的新 [UINavigationBar appearance] 功能向我的应用程序中的 UINavigationBars 添加徽标图像。首先,我想保留默认渐变,但在导航栏中将透明 png 居中。徽标图像的宽度约为 120 像素(240 像素@2x)。

我首先通过设置背景图像来尝试此操作。 setBackgroundImage:forBarMetrics: 的默认行为似乎是平铺图像,并且所有透明部分都显示默认导航栏背景颜色黑色。我还可以通过外观修改器设置背景颜色,并获得纯色背景,但我真的很想获得原始渐变行为,而不需要为其维护单独的图像资源。它还使得在代码中进行调整变得更容易,因为我可以在那里调整色调,而不是在决定更改时重新生成新图像。

我正在尝试使用什么:

UIImage *logoImage = [UIImage imageNamed:@"logoImage"];
[[UINavigationBar appearance] setBackgroundImage:logoImage forBarMetrics:UIBarMetricsDefault];

【问题讨论】:

    标签: image background ios5 uinavigationbar gradient


    【解决方案1】:

    您可以通过两种方式做到这一点。如果您希望始终在导航栏中显示图像,则创建一个图像视图并将其设置为导航栏的子视图:

    [self setLogoImageView:[[UIImageView alloc] init]];
    [logoImageView setImage:[UIImage imageNamed:@"logo.png"]];
    [logoImageView setContentMode:UIViewContentModeScaleAspectFit];
    
    CGRect navFrame = [[navController navigationBar] frame];
    
    float imageViewHeight = navFrame.size.height - 9;
    
    float x_pos = navFrame.origin.x + navFrame.size.width/2 - 111/2;
    
    float y_pos = navFrame.size.height/2 - imageViewHeight/2.0;
    
    CGRect logoFrame = CGRectMake(x_pos, y_pos, 111, imageViewHeight);
    
    [logoImageView setFrame:logoFrame];
    [[[self navigationController] navigationBar] addSubview:logoImageView];
    

    如果你只想在某个view中显示logo,那么设置view的导航项:

    [logoImageView setAutoresizingMask:UIViewAutoresizingFlexibleHeight];
    [[self navigationItem] setTitleView:logoImageView];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多