如何防止大标题折叠 [英] How to prevent Large Title from Collapsing

查看:22
本文介绍了如何防止大标题折叠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题很简单,如何防止大标题导航栏在滚动视图向下滚动时崩溃?

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

这篇关于如何防止大标题折叠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆