gpt4 book ai didi

ios - Swift 中奇怪的导航栏/状态栏行为

转载 作者:行者123 更新时间:2023-11-28 13:00:55 25 4
gpt4 key购买 nike

我正在显示嵌入在 UINavigationController 中的 UITableViewController。我还创建了一个动画,以便在用户向上滚动时将导航栏滑出屏幕,以匹配用户的手指 Action (我提到这一点是因为删除此动画也会删除我的错误,但我不确定为什么)。当用户选择表格的一行时,将显示一个新的(空白)UIViewController,其中隐藏了状态栏。当第二个 Controller 被点击关闭时,我们返回到第一个 Controller 。

在关闭第二个 Controller 后,可能会出现一些非常奇怪的行为,具体取决于在首次选择表格行时第一个 Controller 的表格 View 是否一直滚动到表格顶部。

场景 1: 如果在选择行时表格滚动到最顶部,在第二个 View Controller 之后,导航栏似乎将自身裁剪到其边界被解雇。换句话说,状态栏不再具有与导航栏相同的背景颜色,而是透明的:表格 View 的行现在在状态栏下方可见。

场景 2: 如果在选择行时表格没有滚动到最顶部,在第二个 Controller 之后导航栏/状态栏行为完全正常被解雇,任何挥之不去的奇怪行为都会得到纠正。

scenarios

这是我的(精简)代码:

import UIKit

class ViewControllerTest: UITableViewController {

override func viewDidLoad() {
super.viewDidLoad()
navigationController!.navigationBar.barTintColor = UIColor.grayColor()
navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Add, target: nil, action: nil)
}

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
presentViewController(OtherViewControllerWithNoStatusBar(), animated: false, completion: nil)
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 100
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = UITableViewCell()
cell.textLabel!.text = "item \(indexPath.row)"
return cell
}

var previousScrollViewYOffset : CGFloat = 0

override func scrollViewDidScroll(scrollView: UIScrollView) {
//for navBar hiding
let navBar = navigationController!.navigationBar
var f = navBar.frame;
let size = f.size.height - 21;
let framePercentageHidden = ((20 - f.origin.y) / (f.size.height - 1));
let scrollOffset = scrollView.contentOffset.y;
let scrollDiff = scrollOffset - self.previousScrollViewYOffset;
let scrollHeight = scrollView.frame.size.height;
let scrollContentSizeHeight = scrollView.contentSize.height + scrollView.contentInset.bottom;

if scrollOffset <= -scrollView.contentInset.top {
f.origin.y = 20
} else if (scrollOffset + scrollHeight) >= scrollContentSizeHeight {
f.origin.y = -size
} else {
f.origin.y = min(20, max(-size, f.origin.y - scrollDiff))
}

navBar.frame = f
navBar.tintColor = navBar.tintColor.colorWithAlphaComponent(1 - framePercentageHidden)
self.previousScrollViewYOffset = scrollOffset
}
}

class OtherViewControllerWithNoStatusBar : UIViewController {

override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = UIColor.greenColor()
let label = UILabel(frame: view.bounds)
label.text = "Tap to dismiss!"
view.addSubview(label)
view.addGestureRecognizer(UITapGestureRecognizer(target: self, action: "dismiss"))
}

override func prefersStatusBarHidden() -> Bool {
return true
}

func dismiss() {
presentingViewController!.dismissViewControllerAnimated(false, completion: nil)
}

}

我想知道的是:(1) 为什么会出现场景 1 中的行为,以及 (2) 我该如何解决?

我使用的是 Xcode 7.1,我已经在 iPhone 5 & 6 和 iOS 9.1 上测试过了。

最佳答案

这是我的小建议,为什么你没有尝试使用简单/原生答案使用使用默认导航栏并设置动画显示/隐藏

滑动事件

navigationController?.hidesBarsOnSwipe = true

在选择/点击事件上

   // setting hidesBarsOnTap to true
navigationController?.hidesBarsOnTap = true

附加 Information .

否则使用简单/ native 函数来显示和隐藏动画

self.navigationController?.setNavigationBarHidden(true, animated: true)
UIApplication.sharedApplication().statusBarHidden=true

关于ios - Swift 中奇怪的导航栏/状态栏行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33864563/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com