[英]Why does the UITableView goes below the TabBar?
This is how it currently looks: 当前是这样的:
This is my current code: 这是我当前的代码:
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)
}
It looks liked I have coded it correctly. 看起来我已经正确编码了。 Also in the Storyboard's Attribute Inspector, I have unchecked the Extend Edges: Under Bottom Bar . 同样在情节提要的Attribute Inspector中,我未选中Extend Edges:在底部栏下 。 Any ideas? 有任何想法吗?
Guessing here, but you're probably having an autoresizing mask translated to a constraint. 在这里猜测,但是您可能正在将自动调整大小的蒙版转换为约束。 Breaking your layout as a result. 结果破坏了您的布局。 Try: 尝试:
myView.autoresizingMask = []
myTableView.autoresizingMask = []
// or alternatively
myView.translatesAutoresizingMaskIntoConstraints = false
myTableView.translatesAutoresizingMaskIntoConstraints = false
But it does not matter whether things are set up correctly or not because you are calculating the actual layout by hand. 但这是否设置正确并不重要,因为您是手动计算实际布局的。 Try to utilize Auto Layout instead: 尝试改用自动版式:
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.