如何清除状态栏和导航栏iOS

How to get clear status bar and navigation bar iOS

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

里面我定义的地方returntable查看

        table.contentInsetAdjustmentBehavior = .never

内部视图已加载

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

这是它目前的样子的照片:

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

谢谢!!!

在你

之后
table.contentInsetAdjustmentBehavior = .never

确保还添加:

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

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

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

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
    }
}