当使用大标题并点击状态栏滚动到 UIScrollView
或 UITableView
的顶部时(可能还有 UICollectionView
,没有'我没有测试过)它总是有点过头了。
我在我的 TableView
上启用了刷新,当点击状态栏时,它会像这样显示并保持这种状态,直到我点击屏幕。
我在另一个 ViewController
中有一个 ScrollView
,如果我点击那里的状态栏,它也会滚动得有点远,使导航栏太高。当我点击某处或稍微滚动一点时,这也会恢复正常。
这也只会在我激活大标题时发生,使用普通标题一切正常。
有什么办法解决这个问题吗?
如何重新创建:
- 创建一个新项目,其中包含一个导航 Controller 和一个
UIViewController
,其中包含一个TableView
。 - 将导航 Controller 设置为更喜欢大标题。关闭半透明。在
UIViewController
上设置标题
- 在
TableView
上设置约束以固定到ViewController
的边缘 - 在
ViewController
中为TableView
创建导出 - 实现委托(delegate)并设置行数,例如 100
- 启动应用
- 向下滚动,使大标题变成普通标题
- 点击状态栏使
tableView
滚动到顶部
现在标题不在它应该在的位置,如果你现在向上或向下滚动一点点它会快速回到正常位置。
ViewController
代码:
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
}
}
extension ViewController: UITableViewDelegate, UITableViewDataSource {
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 100
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "TestCell", for: indexPath)
return cell
}
}
最佳答案
好的,所以我找到了问题发生的原因,但没有找到在那个确切的情况下如何解决它。
如果您使用大标题和导航栏半透明设置为关闭的 UITableViewController,则会出现问题。 当您重新打开半透明时,问题就消失了。
如果您在普通的 UIViewController 中使用 TableView,问题总是会发生。
编辑
事实证明,如果您使用的是半透明导航栏,设置“extendedLayoutIncludesOpaqueBars = true”可以解决问题!
关于iOS 11 在使用大标题时无法正常滚动到顶部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50625225/