问题
我的应用程序似乎布局正确,但我无法实现 iOS 7 著名的模糊半透明效果。我的看起来不透明。
期望的效果
我正在尝试获得更明显的模糊效果,例如 Apple 的 Trailers 应用:
半透明
在我的 UINavigationController 子类中,我将导航栏设为半透明:
- (id)initWithRootViewController:(UIViewController *)rootViewController
{
if (self = [super initWithRootViewController:rootViewController]) {
self.navigationBar.translucent = YES;
}
return self;
}
色调颜色
在我的 UIApplicationDelegate 子类中,我设置了导航栏的色调。我发现色调颜色的 alpha 没有区别。也就是说,使用 0.1 的 alpha 不会使条形图变得更加半透明。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[[UINavigationBar appearance] setTintColor:[UIColor greenColor]];
}
边缘
在我的内容 View Controller 中,我将边缘设置为 UIRectEdgeNone
所以顶部不会得到 chopped off通过导航栏。如果我使用默认的 UIRectEdgeAll
,导航栏将永久覆盖我的内容的顶部。即使我忍受了这种异常,UIRectEdgeAll
仍然没有启用半透明效果。
- (void) viewDidLoad
{
[super viewDidLoad];
self.edgesForExtendedLayout = UIRectEdgeNone;
}
编辑:试验边缘
@rmaddy 在评论中指出,问题可能出在 edgesForExtendedLayout 上。我找到了一个 comprehensive tutorial edgesForExtendedLayout并尝试实现它:
- (void) viewDidLoad
{
[super viewDidLoad];
self.edgesForExtendedLayout = UIRectEdgeAll;
self.automaticallyAdjustsScrollViewInsets = YES;
self.extendedLayoutIncludesOpaqueBars = NO;
}
它没有用。首先,没有半透明效果。其次,我的内容顶部被砍掉了。在以下带有上述代码的示例页面中,头像最初被导航栏覆盖,很难滚动到。下拉可以看到头像顶部,松手时页面会自动弹回,头像又会被遮挡。
最佳答案
问题是第三方下拉刷新 View 引起的EGORefreshTableHeaderView ,在 iOS 6 引入系统刷新控件之前被广泛使用。
这个 View 混淆了 iOS 7,让它认为内容比实际的要高。对于 iOS 6 和 7,我有条件地切换到使用 UIRefreshControl .现在导航栏不会砍掉我的内容了。我可以使用 UIRectEdgeAll
使我的内容位于导航栏下方。最后,我用较低的 alpha 为导航栏着色以获得半透明效果。
// mostly redundant calls, because they're all default
self.edgesForExtendedLayout = UIRectEdgeAll;
self.automaticallyAdjustsScrollViewInsets = YES;
self.extendedLayoutIncludesOpaqueBars = NO;
[[UINavigationBar appearance] setTintColor:[UIColor colorWithWhite:0.0 alpha:0.5]];
关于ios - 如何在 iOS 7 的导航栏上获得模糊和半透明的效果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19967737/