【问题标题】:How to get clear status bar and navigation bar iOS如何获得清晰的状态栏和导航栏 iOS
【发布时间】:2021-11-27 09:41:15
【问题描述】:

我有一个表格视图,其中第一个单元格的 imageView 占据了整个单元格。我希望状态栏和导航栏是透明的并显示图像。到目前为止,我的导航栏很清晰,可以根据需要进行查看。我的问题是如何让状态栏以相同的方式运行。我将包含导航栏的代码,但导航栏看起来不错。

在我定义和返回表格视图的地方

        table.contentInsetAdjustmentBehavior = .never

内部视图确实加载了

        self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
        self.navigationController?.navigationBar.shadowImage = UIImage()
        self.navigationController?.navigationBar.isTranslucent = true

这是它目前的样子:

如果可能,请发送程序化建议,因为我没有为此项目使用任何故事板。

谢谢!!!

【问题讨论】:

    标签: ios swift uikit


    【解决方案1】:

    你在正确的轨道上。只要确保您的表格视图覆盖整个超级视图。如果您使用约束,请确保表视图顶部约束附加到超级视图的顶部锚点。

    这是一个透明状态栏和导航栏的工作示例,状态栏下方有表格视图单元格。

    class ViewController: UIViewController, UITableViewDataSource {
    
        private var tableView: UITableView!
    
        override func viewDidLoad() {
            super.viewDidLoad()
            setupNavbar()
            setupTableView()
        }
    
        private func setupNavbar() {
            self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
            self.navigationController?.navigationBar.shadowImage = UIImage()
            self.navigationController?.navigationBar.isTranslucent = true
        }
    
        private func setupTableView() {
            let tableView = UITableView()
            tableView.contentInsetAdjustmentBehavior = .never
            tableView.translatesAutoresizingMaskIntoConstraints = false
            view.addSubview(tableView)
            tableView.contentInsetAdjustmentBehavior = .never
            NSLayoutConstraint.activate([
                tableView.topAnchor.constraint(equalTo: view.topAnchor),
                tableView.bottomAnchor.constraint(equalTo: view.bottomAnchor),
                tableView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
                tableView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
            ])
            tableView.dataSource = self
            self.tableView = tableView
        }
    
        func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return 10
        }
    
        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            let cell = UITableViewCell(style: UITableViewCell.CellStyle.default, reuseIdentifier: "Cell")
            cell.backgroundColor = [UIColor.red, UIColor.gray, UIColor.green, UIColor.blue].randomElement()
            return cell
        }
    }
    

    【讨论】:

    • 就是这样!谢谢!
    【解决方案2】:

    在你之后

    table.contentInsetAdjustmentBehavior = .never
    

    确保还添加:

         tableView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
    

    【讨论】:

      猜你喜欢
      • 2014-09-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多