简体   繁体   English

在NavigationBar的后面添加UIImageView(非背景图片)

[英]Add a UIImageView to the back of NavigationBar (NOT background image)

I am trying to add a UIImageView to the back of a navigation bar. 我试图在导航栏的后面添加一个UIImageView
The reason is because I want to create a UITableView whose navigation bar is actually a picture (with back button on the left) but I want the picture to scroll with the tableview and when the picture is fully scrolled out. 原因是因为我想创建一个UITableView其导航栏实际上是一张图片(左边有后退按钮),但是我希望图片能够与tableview一起滚动,并且当图片完全滚动时。 The navigation bar is shown as per normal. 导航栏按正常显示。
My solution to this problem: 我对这个问题的解决方案:
Add a UIImageView to the top of the UITableView and make the navigation bar transparent. UIImageView添加到UITableView的顶部并使导航栏透明。 Set a contentOffset for the UITableView which is a subclass of UIScrollView so that when the view is presented, it looks like the picture is filling the navigation status bar. 设置一个contentOffsetUITableView是子类UIScrollView这样,当视图被呈现,它看起来像图片填充导航状态栏。
显示视图时,它正是我想要的。 Problem: 问题:
If I scroll up, instead of bouncing back, the transparent status bar is shown (with a color of the background as it is transparent). 如果我向上滚动而不是反弹,则会显示透明状态栏(背景颜色为透明状态)。
如果我向上滚动,而不是反弹,我会看到透明的导航栏。 Possible way to solve this new problem: 解决这个新问题的可能方法:
I was thinking of trying to limit the ScrollView size to get around with problem but failed. 我正在考虑限制ScrollView大小以解决问题但失败了。
So I feel is it possible to add the UIImageView to the "back" of the navigation bar so that it is there without any offset? 所以我觉得可以将UIImageView添加到导航栏的“后面”,以便它没有任何偏移吗? Since that way, my life will be much easier. 从那以后,我的生活将变得更加轻松。
Any suggestions on solving this or another new approach to get the same UI/effect? 有关解决此方法或其他新方法以获得相同UI /效果的任何建议吗?
Related question. 相关问题。

I would do this by adding either a table header or cell at the top of the table which contains your image. 我会通过在表格顶部添加一个包含图像的表格标题或单元格来完成此操作。

Create the table view so that it extends all the way to the top of the screen. 创建表视图,使其一直延伸到屏幕顶部。 Extend Under Top Bars option. 扩展顶部栏选项。 I have not done this with a UITableViewController but I have done this with a UITableView embedded inside a UIViewController 's view with the top constraint set to 0 for the view rather than the top layout guide. 我没有使用UITableViewController做到这一点,但是我已经在UIViewController的视图中嵌入了UITableView ,顶部约束设置为0,而不是顶部布局指南。

Now when you run this your table will fill the whole screen and the top header or cell will be at the top showing your picture. 现在,当您运行此表时,您的表格将填满整个屏幕,顶部标题或单元格将显示在您的图片顶部。

When you scroll you can either use the UIScrollViewDelegate to detect the movement or implement tableView:didEndDisplayingCell:forRowAtIndexPath: 滚动时可以使用UIScrollViewDelegate来检测移动或实现tableView:didEndDisplayingCell:forRowAtIndexPath:

I'm not 100% sure when you want the navigation bar to go non clear. 当你想要导航栏不清楚时,我不是百分百肯定。 If its when the image goes off screen then didEndDisplayingCell should be good. 如果它在图像离开屏幕的时候那么,那么完成显示细胞应该是好的。 If its when the cell bottom passed under the bottom of the navigation bar then scroll view might be your only option. 如果在单元格底部通过导航栏底部下方时,滚动视图可能是您唯一的选择。

This will also bounce as you expect when you pull down and it should snap back to the top. 当你下拉时它也会像你期望的那样反弹,它应该重新弹回到顶部。

Hope this helps. 希望这可以帮助。

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

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