【问题标题】:How to add UIVisualEffectView to UINavigationBar in iOS 8?如何在 iOS 8 中将 UIVisualEffectView 添加到 UINavigationBar?
【发布时间】:2014-09-26 07:58:32
【问题描述】:

我想使用 UIVIsualEffectView 给导航栏添加模糊效果。我有如下自定义导航栏类​​。

头文件

#import <UIKit/UIKit.h>

@interface GSNavigationBar : UINavigationBar
@end

实现文件如下所示

#import "GSNavigationBar.h"

@implementation GSNavigationBar

- (void) awakeFromNib {

     UIVisualEffectView *visualEffectView = [[UIVisualEffectView alloc] initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleExtraLight]];
     visualEffectView.frame = CGRectMake(0, -21, self.frame.size.width , self.frame.size.height + 21);
     [self setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
     [self insertSubview:visualEffectView atIndex:1000];
}
@end

通过上面的代码,我可以实现导航栏的模糊效果。在推动视图控制器时(通过使用“Push”segue),导航栏中的左栏按钮项和右栏项变为非活动状态。为了更清楚,我附上了视图控制器的屏幕截图。


This is view controller #1. All navigation item buttons are clickable
This is view controller #2. We land up here due to push segue from view controller #1. Navigation item buttons are NOT clickable. Also the title goes missing. I had set the title using self.title

【问题讨论】:

  • 为什么要在导航栏添加一个空图像?导航栏已经有模糊效果,你应该使用它。

标签: ios objective-c


【解决方案1】:

带有translucent = YES 的NavigationBar 已经具有模糊效果。

所以就用它吧。

【讨论】:

  • 我修改了代码添加当前我的代码看起来像 - (void) awakeFromNib { self.translucent = YES; } 不会产生预期的效果。我在这里错过了什么吗?
  • 它应该适用于简单的 - (void)awakeFromNib {self.translucent = YES};
  • 对不起 self.translucent = YES;没有产生预期的效果。
  • 可以通过设置控制navigationBar的颜色,self.tintColor = [UIColor someColor];也许这会对你有所帮助
  • 不。 tintColor 改变 UIBarButtons 的颜色。我已经尝试了所有 barTint、tintColor、backgroundColor 以及颜色的不同 alpha。添加 UIVisualEffectView 提供了我正在寻找的完美效果。
【解决方案2】:

这是为了让导航栏完全透明,如果你有模糊的背景相信它会是你需要的效果。

斯威夫特:

func setupNavigationBar() {
    navigationController!.navigationBar.setBackgroundImage(UIImage(), forBarMetrics: .Default)
    navigationController!.navigationBar.shadowImage = UIImage()
    navigationController!.navigationBar.translucent = true
}

目标 C:

- (void) setupNavigationBar{
    [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];

    self.navigationController.navigationBar.shadowImage = [UIImage new];
    self.navigationController.navigationBar.translucent = YES;
}

【讨论】:

    【解决方案3】:
    [self sendSubviewToBack:visualEffectView];
    

    【讨论】:

      【解决方案4】:

      在 UIVisualEffect 上将属性 UserInteractionEnabled 设置为 NO(在 swift 中为 false)。

      所以你应该添加你的代码:

      visualEffectView.userInteractionEnabled = NO
      

      这应该使您的 navigationItems 左右可用

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-07-26
        • 1970-01-01
        相关资源
        最近更新 更多