iOS中的静态/固定导航栏 [英] Static/fixed Navigation Bar in iOS
问题描述
我的应用程序下有一个导航控制器和一个集合视图.有一个问题:我在导航栏中使用大标题,因此其中的所有内容都不是静态的.当我滚动集合视图单元格时,标题(我使用UILabel()
手动创建了它,以便在导航栏内随意移动它)和按钮向上移动,导航栏采用iOS 10导航栏的形式,我的意思是它的高度.您可以在这里看到它:
I have a Navigation Controller and a Collection View under it inside my app. And there is a problem: I use large title inside my Navigation bar, so everything inside is not static. When I scroll the collection view cells, the title (I created it manually using UILabel()
to move it as I want inside the navigation bar) and buttons move up and the navigation bar takes form of iOS 10 navigation bar, I mean its height. You can see it here:
我的导航栏的正常状态为喜欢大标题"打开:
The normal state of my Navigation Bar with "Prefer large titles" On:
当我滚动收藏夹视图"时,一切都会发生:
It happens when I scroll my Collection View, everything goes up:
所以问题很简单:如何使导航条的力保持恒定的高度?我希望它即使在滚动时也变得固定.有什么想法吗?有可能吗?
So the question is simple: how to make the force constant height for the navigation bar? I want it to become fixed even while scrolling. Are there any ideas? Is it possible?
第二个问题,如果第一个不可能,那么:解决我的问题的另一种方法是使优先选择大标题"的导航栏变大.我尝试了这段代码:
And the second question, if the first is impossible: Another solution for my problem is to make the Navigation Bar with "Prefer large titles" Off bigger. I tried this code:
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
let height: CGFloat = 50 //whatever height you want to add to the existing height
let bounds = self.navigationController!.navigationBar.bounds
self.navigationController?.navigationBar.frame = CGRect(x: 0, y: 0, width: bounds.width, height: bounds.height + height)
}
,但仅适用于大标题.那么如何使导航栏更大?
but it worked only for large titles. So how can I make the navigation bar bigger?
推荐答案
是的,您可以将其修复.如果视图层次结构中的第一个视图不是CollectionView/TableView (ScrollView)
,它将不会滚动.
Yes, you can make it fixed. It will not scroll if the very first view in the view hierarchy is not a CollectionView/TableView (ScrollView)
.
使用情节提要/XIB:
请考虑以下图像,其中在scene
中添加了tableView
和button
.此处navigation bar
将在tableView
滚动时折叠,因为tableView
是viewController
的containerView
层次结构中附加到导航栏的第一个视图.
Consider the following image where a tableView
and button
are added in scene
. Here the navigation bar
will collapse on scroll of tableView
because tableView
is the very first view in viewController
's containerView
hierarchy attached to the navigation bar.
现在要固定导航栏,如果我们仅按以下方式更改tableView
和button
的顺序,它将禁用navigation bar
的折叠.
Now to make the navigation bar fixed, if we just change the order of tableView
and button
as below, it will disable the collapsing of navigation bar
.
要更改view
的顺序,必须单击,按住并向上/向下移动.
To change the order of the view
, you have to click, hold and move up/down.
如果此场景中只有CollectionView
,则可以在顶部添加占位符view
,并将其height
设置为zero
,如下所示,
If you have only CollectionView
in this scene then you can add a placeholder view
at the top and set its height
to zero
as below,
以编程方式:
如果要以编程方式设置视图,则只需要在顶部添加一个占位符视图,或者在添加其他视图之后添加tableView/collection
.
If you are setting up view's programmatically then you just need to add a placeholder view at the top or add tableView/collection
after adding other views.
例如,
self.view.addSubview(UIView(frame: .zero))
self.view.addSubview(tableView) // or collectionView
这篇关于iOS中的静态/固定导航栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!