我有一個表格視圖,其中第一個單元格的 imageView 占據了整個單元格。我希望狀態欄和導航欄透明并顯示影像。到目前為止,我已經根據需要清晰地查看了導航欄。我的問題是如何讓狀態欄以相同的方式運行。我將包含導航欄的代碼,但導航欄看起來不錯。
在我定義和回傳表視圖的地方
table.contentInsetAdjustmentBehavior = .never
內部視圖確實加載
self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
self.navigationController?.navigationBar.shadowImage = UIImage()
self.navigationController?.navigationBar.isTranslucent = true
這是它目前的外觀圖片:

如果可能,請發送程式化建議,因為我沒有為此專案使用任何故事板。
謝謝!!!
uj5u.com熱心網友回復:
你在這里走在正確的軌道上。只要確保你的表格視圖覆寫了整個超級視圖。如果您使用約束,請確保表視圖頂部約束附加到超級視圖的頂部錨點。
這是透明狀態欄和導航欄的作業示例,狀態欄下帶有表格視圖單元格。
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
}
}
uj5u.com熱心網友回復:
在你之后
table.contentInsetAdjustmentBehavior = .never
確保還添加:
tableView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/368539.html
