【发布时间】:2020-05-15 21:19:15
【问题描述】:
我正在使用UITableViewController,它在滚动时会使导航栏消失。现在,当用户滑动表格视图时隐藏导航栏时,可以在状态栏下方看到单元格的内容...
为了解决这个问题,我尝试插入UIView 来模拟状态栏的背景,一切正常,但问题是当我关闭UITableViewController 时,状态栏的背景视图并未从超级视图
现在我的代码是这样的,你能帮我理解我错在哪里吗?为什么我不能从 superview 中删除 UIView?
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
setupStatusBarView()
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
navigationController?.navigationBar.isHidden = true
UIApplication.shared.windows.first?.viewWithTag(1)?.removeFromSuperview()
}
//MARK: - Setup Status Bar View
func setupStatusBarView() {
let height = view.window?.windowScene?.statusBarManager?.statusBarFrame.height ?? 0
let statusBarView = UIView()
statusBarView.frame = CGRect(x: 0, y: 0, width: view.frame.width, height:height+5)
statusBarView.backgroundColor = .systemBackground
statusBarView.tag = 1
UIApplication.shared.windows.first?.addSubview(statusBarView)
}
【问题讨论】:
-
它对我来说工作正常
-
在添加
StatusBarView之前,请检查状态栏视图是否已经存在...可能您添加了两次...删除了一次
标签: ios swift uitableview uiview statusbar