简体   繁体   English

UICollectionView的透明UINavigationBar问题

[英]Transparent UINavigationBar issue with UICollectionView

I've created a simple project to test how to make UINavigationBar transparent. 我创建了一个简单的项目来测试如何使UINavigationBar透明。 In this project I have two UIViewController. 在这个项目中,我有两个UIViewController。 The first view controller shows a big button which push the second view controller into navigation controller. 第一视图控制器显示一个大按钮,该按钮将第二视图控制器推入导航控制器。

The second view controller is a UIViewController which contains an UICollectioView (it isn't a UICollectionViewController). 第二个视图控制器是一个UIViewController,其中包含一个UICollectioView(它不是UICollectionViewController)。

In the app delegate I wrote this code into application:didFinishLaunchingWithOptions 在应用程序委托中,我将此代码写入了application:didFinishLaunchingWithOptions

UINavigationBar.appearance().setBackgroundImage(UIImage(), forBarMetrics: .Default)
UINavigationBar.appearance().shadowImage = UIImage()
UINavigationBar.appearance().backgroundColor = UIColor(red: 0.0, green: 0.0, blue: 0.0, alpha: 0.0)
UINavigationBar.appearance().translucent = true

It works like a charm for the first UIViewController (the navigation bar is transparent), but when I move to the other UIViewController (with UICollectionView) the navigation bar is not transparent and when I come back to previous UIViewController still remains not transparent! 对于第一个UIViewController来说,它的工作原理就像是一种魅力(导航栏是透明的),但是当我移到另一个UIViewController(带有UICollectionView)时,导航栏是不透明的,而当我回到以前的UIViewController时,它仍然不透明!

Any suggestion? 有什么建议吗?

PS: Just to be clear, my expected result is that I want to see cells even when I scroll up and they pass under the navigation bar. PS:请明确一点,我的预期结果是,即使向上滚动并且它们在导航栏下方通过,我也希望看到它们。 I don't know if I was clear. 我不知道我是否清楚。

故事板

As i do not have 50 reputation yet i cannot comment on the question, but i replicated your project in Xcode and i found no errors. 由于我没有50的声誉,所以我无法对此问题发表评论,但是我在Xcode中复制了您的项目,但没有发现错误。 I got the following screens (notice the translucent navigation bar): 我得到以下屏幕(注意半透明的导航栏):

第一视图控制器

第二视图控制器

To create these screens the following actions where taken so this is where something could have gone wrong: 要创建这些屏幕,请执行以下操作,以防止出现问题:

  • Add code to AppDelegate 将代码添加到AppDelegate
  • Embed FirstViewController in NavigationController 将FirstViewController嵌入NavigationNavigator中
  • Add SecondViewController 添加SecondViewController
  • Add button to FirstViewController 将按钮添加到FirstViewController
  • Add segue between 'Button' and SecondViewController 在“按钮”和SecondViewController之间添加序列
  • Add CollectionView to SecondViewController 将CollectionView添加到SecondViewController
  • Add Constraints to 'Button' and 'CollectionView' 将约束添加到“按钮”和“集合视图”

This might sound stupid but in the AppDelegate did you place your code before the: 这听起来可能很愚蠢,但是您是否在AppDelegate中将代码放置在:

return true

statement? 声明?

I spent 24 hours to figure out the problem! 我花了24小时找出问题所在! It was a stupid bad constraint! 这是一个愚蠢的坏约束! The UICollectionView had a constraint to the Top Layout Guide instead of View of UIViewController! UICollectionView对顶部布局指南有约束,而不是UIViewController的视图!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM