我有一个View Controller,它有一个子表视图控制器和一个叠加视图。
View Controller
|_ TableView
|_ Overlay view
|_ Label
启用语音播放后,单元格将处于焦点。当标签聚焦时,单元格被推下,在表格视图中留下很大的间隙。
只有在开启配音时才会发生这种情况
当细胞聚焦时,事情还可以,但是当标签聚焦时,同样的事情也会发生。
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
showRedTableViewController()
addOverlay()
}
private func showRedTableViewController() {
let redTableViewController = RedTableViewController()
guard let redView = redTableViewController.view else {
return
}
addChildViewController(redTableViewController)
view.addSubview(redView)
redView.translatesAutoresizingMaskIntoConstraints = false
redView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
redView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
redView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
redView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
redTableViewController.didMove(toParentViewController: self)
}
private func addOverlay() {
let overlay = UIView()
overlay.backgroundColor = .green
let label = UILabel()
label.text = "Overlay text"
overlay.translatesAutoresizingMaskIntoConstraints = false
label.translatesAutoresizingMaskIntoConstraints = false
overlay.addSubview(label)
view.addSubview(overlay)
//Label Layout
label.centerXAnchor.constraint(equalTo: overlay.centerXAnchor).isActive = true
label.centerYAnchor.constraint(equalTo: overlay.centerYAnchor).isActive = true
//Overlay Layout
overlay.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
overlay.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
overlay.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
overlay.heightAnchor.constraint(equalToConstant: 100).isActive = true
}
}
class RedTableViewController : UITableViewController {
private let cellID = "Cell"
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .red
tableView.register(UITableViewCell.self, forCellReuseIdentifier: cellID)
}
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: cellID)!
cell.textLabel?.text = "Cell \(indexPath.row)"
return cell
}
}