[英]Why does the UITableView goes below the TabBar?
當前是這樣的:
這是我當前的代碼:
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor(red: 4/255, green: 4/255, blue: 4/255, alpha: 1.0)
self.navigationController?.navigationBar.barStyle = .black
self.navigationController?.navigationBar.tintColor = UIColor.white
self.navigationItem.title = "Test"
self.navigationController?.navigationBar.prefersLargeTitles = true
// Get main screen bounds
let screenSize: CGRect = UIScreen.main.bounds
let screenWidth = screenSize.width
let screenHeight = screenSize.height
myView.frame = CGRect(x: 0, y: 0, width: screenWidth, height: 150)
myView.backgroundColor = .red
self.view.addSubview(myView)
myTableView.frame = CGRect(x: 0, y: myView.frame.size.height, width: screenWidth, height: screenHeight-myView.frame.size.height-(navigationController?.navigationBar.frame.size.height)!-(tabBarController?.tabBar.frame.size.height)!)
print("SCREEN: \(screenHeight)")
print("TABLEVIEW: \(myTableView.frame.size.height)")
myTableView.dataSource = self
myTableView.delegate = self
myTableView.backgroundColor = .blue
myTableView.layer.borderWidth = 3
myTableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
self.view.addSubview(myTableView)
}
看起來我已經正確編碼了。 同樣在情節提要的Attribute Inspector中,我未選中Extend Edges:在底部欄下 。 有任何想法嗎?
在這里猜測,但是您可能正在將自動調整大小的蒙版轉換為約束。 結果破壞了您的布局。 嘗試:
myView.autoresizingMask = []
myTableView.autoresizingMask = []
// or alternatively
myView.translatesAutoresizingMaskIntoConstraints = false
myTableView.translatesAutoresizingMaskIntoConstraints = false
但這是否設置正確並不重要,因為您是手動計算實際布局的。 嘗試改用自動版式:
myView.translatesAutoresizingMaskIntoConstraints = false
self.view.addSubview(myView)
myView.topAnchor.constraint(equalTo: self.view.topAnchor).isActive = true
myView.leadingAnchor.constraint(equalTo: self.view.leadingAnchor).isActive = true
myView.trailingAnchor.constraint(equalTo: self.view.trailingAnchor).isActive = true
myView.heightAnchor.constraint(equalToConstant: 150).isActive = true
myTableView.translatesAutoresizingMaskIntoConstraints = false
self.view.addSubview(myTableView)
myTableView.topAnchor.constraint(equalTo: myView.bottomAnchor).isActive = true // making myTableView to lie just below myView
myTableView.leadingAnchor.constraint(equalTo: self.view.leadingAnchor).isActive = true
myTableView.trailingAnchor.constraint(equalTo: self.view.trailingAnchor).isActive = true
myTableView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor).isActive = true
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.