【问题标题】:How to prevent Large Title from Collapsing如何防止大标题折叠
【发布时间】:2018-11-27 15:18:02
【问题描述】:

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

我的导航必须始终有一个大导航栏...所以当滚动视图滚动时,导航栏不应该折叠起来,它应该保持相同的大小,我该怎么做?

这就是我设置 largeTitle 首选项的方式

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])

}

【问题讨论】:

    标签: ios swift uinavigationcontroller uinavigationbar uinavigationitem


    【解决方案1】:

    我想出的解决方法是添加一个不是CollectionView/TableView 的占位符视图作为ViewController's 基本视图中的第一个视图。这第一个视图将附加到 safeArea 的顶部,高度可以为零。

    使用 Storyboard/Xib:

    查看下面的屏幕截图,了解带有约束的视图

    接下来添加另一个UIView 作为TableView/CollectionView 的容器视图。此容器的顶部将附加到占位符视图的底部。有关容器视图和TableView/CollectionView 的约束,请参见下面的屏幕截图。

    这里的关键是视图层次结构中的第一个视图,因为navigation bar 将检查它以设置折叠效果。一旦它没有找到CollectionView/TableView,它就不会在滚动时折叠。

    以编程方式:

    如果您以编程方式设置视图,那么您只需要在顶部添加一个占位符视图。

    例如,

    self.view.addSubview(UIView(frame: .zero))
    self.view.addSubview(tableView) // or collectionView
    

    【讨论】:

      【解决方案2】:

      为了防止大型磁贴导航栏折叠,只需在 viewDidLoad 方法中向 UIViewController 添加第二个视图。

      view.addSubview(UIView())
      

      无论出于何种原因,这都会破坏 UIScrollView 和导航栏之间的链接。

      【讨论】:

        【解决方案3】:

        如果有人从 SwiftUI 土地来到这里,这是让您的列表保持大标题模式的好方法。

        // your content view ...
        
            var body: some View {
                VStack {
                    PreventCollapseView()
                    List {
                        ForEach(things, id: \.self) { thing in
                            Text(thing.name)
                        }
                    }
                }
                .navigationBarTitle("All the things")
            }
        
        
        // end of view
        
        
        struct PreventCollapseView: View {
        
            private var mostlyClear = Color(UIColor(white: 0.0, alpha: 0.0005))
        
            var body: some View {
                Rectangle()
                    .fill(mostlyClear)
                    .frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: 1)
            }
        }
        
        

        【讨论】:

        • 这太好了,谢谢!我设置了VStack(spacing; 0),让内容尽可能接近PreventCollapseView
        • 不幸的是,如果使用列表中的导航链接,这会导致该行在导航返回后保持突出显示。 iOS 15 之前的已知 iOS 错误。stackoverflow.com/questions/63934037/…
        【解决方案4】:

        如果您在情节提要上创建了其他视图,则只需在 viewDidLoad 上调用以下方法。

        private func preventLargeTitleCollapsing() {
            let dummyView = UIView()
            view.addSubview(dummyView)
            view.sendSubviewToBack(dummyView)
        }
        

        我原来的答案是here

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2019-12-31
          • 1970-01-01
          • 2015-10-03
          • 2019-09-02
          • 2020-02-01
          • 2014-05-05
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多