xcode 滚动时缩小大标题(不是 UITableViewController)iOS 11

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46703469/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 10:21:31  来源:igfitidea点击:

Shrink large title when scrolling (not UITableViewController) iOS 11

iosswiftxcodeios11xcode9

提问by Jonathan Solorzano

I have got a View Controller embedded in a Navigation Controller with prefers large titles option set to true; inside the View Controller there's a Scroll View.

我有一个嵌入在导航控制器中的视图控制器,首选大标题选项设置为 true;在视图控制器中有一个滚动视图。

I want to make the nav bar shrink when scrolling.

我想让导航栏在滚动时缩小。

How could I achieve this?

我怎么能做到这一点?

XCode 9, Swift 4, iOS 11

XCode 9、Swift 4、iOS 11

回答by 93sauu

I have not achieved this using a UIScrollView but I achieved it with other ViewControllers using a UITableView as first view.

我没有使用 UIScrollView 实现这一点,但我使用 UITableView 作为第一个视图与其他 ViewControllers 实现了它。

If the tableView is not the first view, the large title fails to hide automatically. You most likely need to make sure your tableView is the first element in the main view's subviews array.

如果 tableView 不是第一个视图,则无法自动隐藏大标题。您很可能需要确保您的 tableView 是主视图子视图数组中的第一个元素。

enter image description here

在此处输入图片说明

I hope that this solves your problem.

我希望这能解决你的问题。

回答by Kamil Szostakowski

Have you tried something like this? It converts the large title display mode when the content is scrolled up.

你有没有尝试过这样的事情?当内容向上滚动时,它会转换大标题显示模式。

class P1ViewController: UIViewController, UIScrollViewDelegate
{
    var canTransitionToLarge = false
    var canTransitionToSmall = true    

    func scrollViewDidScroll(_ scrollView: UIScrollView)
    {
        if canTransitionToLarge && scrollView.contentOffset.y <= 0 {
            UIView.animate(withDuration: 0.5) {
                self.navigationItem.largeTitleDisplayMode = .always
            }
            canTransitionToLarge = false
            canTransitionToSmall = true
        }
        else if canTransitionToSmall && scrollView.contentOffset.y > 0 {
            UIView.animate(withDuration: 0.5) {
                self.navigationItem.largeTitleDisplayMode = .never
            }
            canTransitionToLarge = true
            canTransitionToSmall = false
        }
    }
}

回答by billm

Setting prefersLargeTitles in code did work for me to make the NavBar title shrink and grow as you scroll. I did notice if you set the property through InterfaceBuilder, the shrinking feature didn't work.

在代码中设置 prefersLargeTitles 确实对我有用,可以使 NavBar 标题随着滚动而缩小和增长。我确实注意到如果您通过 InterfaceBuilder 设置属性,则收缩功能不起作用。

IB Property Inspector

IB财产检查员

Instead set in code like

而是在代码中设置

    self.navigationController?.navigationBar.prefersLargeTitles = true

回答by billm

This way is worked for me. i followed these steps: 1- Moving the TableView to top of the list (under safe area) in view controller scene. 2- Making FirstView and Loaded by FirstViewContoller.

这种方式对我有用。我按照以下步骤操作: 1- 将 TableView 移动到视图控制器场景中的列表顶部(在安全区域下)。2- 制作 FirstView 并由 FirstViewContoller 加载。

The screenshot of the solution

解决方案截图

回答by avdyushin

I had the similar problem, is was because of many UIScrollView-based view inside view controller and I was scrolling not first in hierarchy. Once it's only one it works fine with UIScrollView, WKWebViewor UITextViewwithout any line of code.

我遇到了类似的问题,是因为UIScrollView视图控制器中有许多基于视图,而且我不是在层次结构中首先滚动。一旦它只有一个,它就可以正常工作UIScrollViewWKWebView或者UITextView没有任何代码行。

回答by Philippe H. Regenass

I used dsk1306 solutions, see above. But in my case I had a WebView in the UIViewController with the large title.

我使用了 dsk1306 解决方案,见上文。但就我而言,我在 UIViewController 中有一个带有大标题的 WebView。

So I had to set the UIWebView ScrollView delegate:

所以我不得不设置 UIWebView ScrollView 委托:

self.webView.scrollView.delegate = self

and

extension MyViewController: UIScrollViewDelegate {

func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
    if #available(iOS 11.0, *) {
        UIView.animate(withDuration: 0.5, animations: {
            self.navigationController?.navigationBar.prefersLargeTitles = (velocity.y < 0)
        })
    }
}

}

回答by Roman

Try something like this:

尝试这样的事情:

    extendedLayoutIncludesOpaqueBars = true
    scrollView.translatesAutoresizingMaskIntoConstraints = false       
    view.addSubview(scrollView)
    NSLayoutConstraint.activate([
        scrollView.topAnchor.constraint(equalTo: view.topAnchor),
        scrollView.leftAnchor.constraint(equalTo: view.leftAnchor),
        scrollView.bottomAnchor.constraint(equalTo: view.bottomAnchor),
        scrollView.rightAnchor.constraint(equalTo: view.rightAnchor)
        ])

回答by DazChong

I tested with storyboard approach, it is working. Make sure to set the scrollView & it's subviews Constraints.

我用故事板方法进行了测试,它正在工作。确保设置 scrollView 及其子视图 Constraints

Here with the test sample XCode project for references: https://github.com/DazChong/LargeTitleShrinkOnScrollView

这里以测试样例 XCode 项目作为参考:https: //github.com/DazChong/LargeTitleShrinkOnScrollView

回答by dsk1306

I've used something that is similar to Kamil Szostakowski's answer for view with UICollectionView.

我使用了类似于Kamil Szostakowski 的 UICollectionView视图的答案。

func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
    UIView.animate(withDuration: 0.5, animations: {
        self.navigationController?.navigationBar.prefersLargeTitles = (velocity.y < 0)
    })
}

It may be not so smooth but I haven't found better option yet.

它可能不是那么顺利,但我还没有找到更好的选择。

回答by vanab

If u have problem collapse root navigationBar from presented VC's scrollView, can try this: Add UIScrollView to RootVC. Important - UIScrollView must be root view(at index 0). In presentedVC we can use scrollViewDidScroll and set offset on our root UIScrollView. see herewith gif and code RootVC view hierarchy: screenshot(sorry for link, not enough reputation)

如果您在从呈现的 VC 的 scrollView 折叠根导航栏时遇到问题,可以尝试以下操作:将 UIScrollView 添加到 RootVC。重要 - UIScrollView 必须是根视图(在索引 0 处)。在presentedVC 中,我们可以使用scrollViewDidScroll 并在我们的根UIScrollView 上设置偏移量。在这里看到gif 和代码 RootVC 视图层次结构: 截图(抱歉链接,没有足够的声誉)

in presentedVC try this

在presentVC中试试这个

func scrollViewDidScroll(_ scrollView: UIScrollView) {
        if #available(iOS 11.0, *) {
            let offset = scrollView.contentOffset
            rootVC.scrollView.contentOffset = offset
        }
}