Swift UITableViewの一番上に余白が生じてしまう

実現したいこと

セーフエリアにもセルが表示されるようにしたい

発生している問題・分からないこと

余白を削除するコードを書いてみたが機能していない

該当のソースコード

Swift

1import UIKit2 3final class LPViewController: UIViewController {4 5 private lazy var tableView: UITableView = {6 UITableView().apply {7 $0.translatesAutoresizingMaskIntoConstraints = false8 $0.registerNibCell(MainHeaderImageCell.self)9 $0.delegate = self10 $0.dataSource = self11 $0.backgroundColor = .red 12 $0.sectionHeaderTopPadding = 0.013 $0.separatorStyle = .none14 $0.contentInset = .zero 15 $0.tableHeaderView = nil16 $0.sectionHeaderHeight = 017 }18 }()19 20 override func viewDidLoad() {21 super.viewDidLoad()22 23 view.addSubview(tableView)24 25 NSLayoutConstraint.activate([26 tableView.topAnchor.constraint(equalTo: view.topAnchor),27 tableView.leadingAnchor.constraint(equalTo: view.leadingAnchor),28 tableView.trailingAnchor.constraint(equalTo: view.trailingAnchor),29 tableView.bottomAnchor.constraint(equalTo: view.bottomAnchor)30 ])31 }32 33}34 35extension LPViewController: UITableViewDelegate {36 func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {37 return UIView()38 }39 40 func tableView(_ tableView: UITableView, estimatedHeightForHeaderInSection section: Int) -> CGFloat {41 return 0.042 }43}44 45extension LPViewController: UITableViewDataSource {46 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {47 return 148 }49 50 func numberOfSections(in tableView: UITableView) -> Int {51 return 152 }53 54 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {55 let headerCell = tableView.dequeueCell(MainHeaderImageCell.self, for: indexPath)56 return headerCell 57 }58 59 func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {60 return 0.0161 }62 63}64

試したこと・調べたこと

上記の詳細・結果

$0.contentInset = UIEdgeInsets(top: -48, left: 0, bottom: 0, right: 0)
のようなコードを入れると余白が消えるが、正しい方法とは思わないので、良い方法があれば教えてください。

補足

特になし

コメントを投稿

0 コメント