How to prevent Large Title from Collapsing(如何防止大标题折叠)
问题描述
问题很简单,如何防止大标题导航栏在滚动视图向下滚动时崩溃?
The question is simple, how can I prevent a Large Title Navigation Bar from collapse when a scrollview scrolls down?
我的导航必须始终有一个大导航栏...所以当滚动视图滚动时,导航栏不应该折叠起来,它应该保持相同的大小,我该怎么做?
My navigation must have a large navigation bar at all times... so when a scrollview scroll, the navigation bar shouldn't collapse up, it should stay the same size, how can I do that?
这就是我设置 largeTitle 首选项的方式
This is how I set the largeTitle preferences
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.navigationItem.hidesBackButton = true
presenter.expandForSimulatorLayoutIfNeeded()
}
func expandForSimulatorLayoutIfNeeded(){
if !isExpanded{
topMenu = TopMenu(frame: expandedNavigationFrame, interactor: interactor)
oldNavigationBarFrame = navigationBar.frame
self.navigationBar.addSubview(topMenu)
}
if #available(iOS 11.0, *) {
self.navigationBar.prefersLargeTitles = true
} else {
self.navigationBar.frame = expandedNavigationFrame
}
let topConstraint = NSLayoutConstraint(item: topMenu, attribute: .top, relatedBy: .equal, toItem: navigationBar, attribute: .top, multiplier: 1, constant: 0)
let leadingConstraint = NSLayoutConstraint(item: topMenu, attribute: .leading, relatedBy: .equal, toItem: navigationBar, attribute: .leading, multiplier: 1, constant: 0)
let widthConstraint = NSLayoutConstraint(item: topMenu, attribute: .width, relatedBy: .equal, toItem: self.navigationBar, attribute: .width, multiplier: 1, constant: 0)
let bottomConstraint = NSLayoutConstraint(item: topMenu, attribute: .bottom, relatedBy: .equal, toItem: navigationBar, attribute: .bottom, multiplier: 1, constant: 0)
topMenu.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([leadingConstraint, widthConstraint, topConstraint, bottomConstraint])
}
推荐答案
我想出的解决方法是添加一个不是 CollectionView/TableView 的占位符视图作为 中的第一个视图ViewController 的 基本视图.这第一个视图将附加到 safeArea 的顶部,高度可以为零.
A workaround i figured out is to add a placeholder view that is not CollectionView/TableView as the very first view in ViewController's base view. This first view will be attached to the top of the safeArea, height can be zero.
使用 Storyboard/Xib:
查看下面的屏幕截图,了解带有约束的视图
See the below screenshot for this view with constraints
接下来添加另一个 UIView 作为 TableView/CollectionView 的容器视图.此容器的顶部将附加到占位符视图的底部.有关容器视图和 TableView/CollectionView 的约束,请参见下面的屏幕截图.
Next add another UIView to serve as a container view for your TableView/CollectionView. This container's top will be attached to bottom of the placeholder view. See the below screenshot for constraints of container view and TableView/CollectionView.
这里的关键是视图层次结构中的第一个视图,因为 导航栏 将检查它以设置折叠效果.一旦它没有找到它作为 CollectionView/TableView,它就不会在滚动时折叠.
The key here is the first view in the view hierarchy as the navigation bar will check that to set the collapsing effect. Once it does not find it as a CollectionView/TableView, it will not collapse on scrolling.
以编程方式:
如果您以编程方式设置视图,那么您只需要在顶部添加一个占位符视图.
If you are setting up view's programmatically then you just need to add a placeholder view at the top.
例如,
self.view.addSubview(UIView(frame: .zero))
self.view.addSubview(tableView) // or collectionView
这篇关于如何防止大标题折叠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何防止大标题折叠
基础教程推荐
- navigator.geolocation.getCurrentPosition 在 Android 浏览器上 2022-01-01
- Android:getLastKnownLocation(LocationManager.NETWORK_PROVIDER 2022-01-01
- iOS4 创建后台定时器 2022-01-01
- NSString intValue 不能用于检索电话号码 2022-01-01
- Cocos2d iPhone 非矩形精灵触摸检测 2022-01-01
- AdMob 广告未在模拟器中显示 2022-01-01
- libGDX 从精灵或纹理中获取像素颜色 2022-01-01
- 通过重定向链接在 Google Play 中打开应用 2022-01-01
- 如何从 logcat 中删除旧数据? 2022-01-01
- iPhone - 获取给定地点/时区的当前日期和时间并将其与同一地点的另一个日期/时间进行比较的正确方法 2022-01-01
