xcode iOS 11 大标题导航栏捕捉而不是平滑过渡

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/48039046/
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:29:48  来源:igfitidea点击:

iOS 11 large title navigation bar snaps instead of smooth transition

swiftxcodeuitableviewuiviewcontrollerios11

提问by Villarrealized

I'm facing an issue where the large title navigation bar collapses very abruptly when scrolling on a UITableView embedded inside of a UIViewController. The problem seems to only occur when scrolling up on the screen. When scrolling down on the screen, the title transitions smoothly to being big again, but not vice versa.

我面临一个问题,即在嵌入 UIViewController 的 UITableView 上滚动时,大标题导航栏会非常突然地折叠。该问题似乎仅在屏幕上向上滚动时发生。在屏幕上向下滚动时,标题会平滑地再次变大,但反之则不然。

This issue does NOT occur if using a UITableViewController.

如果使用 UITableViewController,则不会发生此问题。

Here is the normal, expected behavior when scrolling inside a UITableViewController.

这是在 UITableViewController 中滚动时的正常预期行为。

iOS 11 UITableView inside of a UIViewController (broken abrupt transition when scrolling)

UIViewController 中的 iOS 11 UITableView(滚动时中断的突然转换)

And here is the broken, abrupt transition when using a UITableView inside of a UIViewController.

这是在 UIViewController 中使用 UITableView 时出现的中断的、突然的转换。

iOS 11 UITableViewController (transitioning properly)

iOS 11 UITableViewController(正确过渡)

Here is the code for the broken implementation:

这是损坏的实现的代码:

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 12
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "Basic", for: indexPath)

        cell.textLabel?.text = "Title \(indexPath.row)"

        return cell
    }

}

The Navigation Bar has Prefers Large Titleschecked and the Navigation Item has Large Titleset to Automatic.

导航栏已选中Prefers Large Titles,并且导航项的Large Title设置为 Automatic。

The code and configuration for both examples above is exactly the same except for the one being a UITableViewController vs. a UITableView inside of a UIViewController.

上面两个示例的代码和配置完全相同,除了一个是 UITableViewController 与 UIViewController 内部的 UITableView。

I have also observed that the broken behavior does NOT occur if the contents of the UITableView does not exceed the view's height. But once there are more cells than can fit on screen, it breaks.

我还观察到,如果 UITableView 的内容不超过视图的高度,则不会发生损坏的行为。但是一旦有更多的单元格超过了屏幕上的容量,它就会崩溃。

Any idea if I'm doing something wrong or if this is an iOS 11 bug?

知道我做错了什么还是这是 iOS 11 的错误?

回答by Soolar

I faced same issue - I had UIViewControllerembedded in UINavigationController, the UIViewControllerhad tableview with leading, trailing, top, bottom constraints to safe area. The whole tableview behaved jumpy / snappy. The trick was to change top constraint of tableview to superview.

我面临着同样的问题-我已经UIViewController嵌入UINavigationController,则UIViewController有tableview中与前置,顶部,底部制约安全区域。整个 tableview 表现得活泼/活泼。诀窍是将 tableview 的顶部约束更改为superview

Here I recorded changing the constraint tableview iOS 11 bug constraint change

这里我记录了更改约束 tableview iOS 11 bug 约束更改

回答by Filippo

Put this line of code on your UIViewController containing UITableView and works fine for me.

将这行代码放在包含 UITableView 的 UIViewController 上,对我来说效果很好。

Swift

迅速

extendedLayoutIncludesOpaqueBars = true;

Objective-C

目标-C

self.extendedLayoutIncludesOpaqueBars = YES;

Set UITableView AutoLayout top space to "Superview" instead of "Safe Area"

将 UITableView AutoLayout 顶部空间设置为“Superview”而不是“Safe Area”

回答by fl034

I had a similar looking issue and solved it by removing UINavigationBar.appearance().isTranslucent = falsefrom my AppDelegate.

我有一个类似的问题,并通过UINavigationBar.appearance().isTranslucent = false从我的 AppDelegate 中删除来解决它。

UPDATE: If you don't want translucency, you should enable extendedLayoutIncludesOpaqueBars. That way all the glitches are fixed.

更新:如果你不想要半透明,你应该启用extendedLayoutIncludesOpaqueBars. 这样所有的故障都修复了。

回答by FarouK

In Interface Builder:

在界面生成器中:

  1. Select your "viewController".
  2. Attribute Inspector tick "Under Opaque Bars" and "Under Top Bars".
  3. Make your top constraints of your scrollView second Item to "SuperView" not "SafeArea".
  1. 选择您的“视图控制器”。
  2. 属性检查器勾选“在不透明条下”和“在顶部条下”。
  3. 将 scrollView 第二项的最高约束设为“SuperView”而不是“SafeArea”。

It worked with me.

它和我一起工作。

Reference

参考

回答by Yongqiang Zhou

Soolar's answer is right, but I don't know how to fix it in storyboard. Finally I solved the problem with Roman's solution in another question, by adding:

Soolar 的回答是正确的,但我不知道如何在故事板中修复它。最后我在另一个问题中用 Roman 的解决方案解决了这个问题,添加:

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

in viewDidLoad.

viewDidLoad.

Original answer: https://stackoverflow.com/a/48321447/1386369

原答案:https: //stackoverflow.com/a/48321447/1386369