Tableview子视图覆盖UIView子类

时间:2018-10-07 12:57:39

标签: ios swift uitableview uiview

我有一个UIView子类,该子类应“浮动”在放置整个屏幕的TableView上方。在与TableView相同的ViewController中进行设置时,UIView子视图显示为应有的状态。我试图使该元素可重用,所以将其移至UIView的单独类。现在,UIView显示在TableView下面。实现BringSubviewToFront()或sendSubviewToBack()无法正常工作,并且没有任何变化。

以下是简化代码:

UIView类

 class SomeUIView: UIView {
    override init(frame: CGRect) {
    super.init(frame: frame)
    self.translatesAutoresizingMaskIntoConstraints = false
    self.addSubview(someUIView)
    setupSomeUIView() //autolayout.etc 
    }

    let someUIView: UIView = {
     //here goes customisation
     return UIView
    }

    func setupSomeUIView(){
      someUIView.translatesAutoresizingMaskIntoConstraints = false
      someUIView.leftAnchor.constraint(equalTo: self.safeAreaLayoutGuide.leftAnchor, constant: 7).isActive = true
      someUIView.topAnchor.constraint(equalTo: self.safeAreaLayoutGuide.topAnchor, constant: 10).isActive = true
      someUIView.rightAnchor.constraint(equalTo: self.safeAreaLayoutGuide.rightAnchor, constant: -7).isActive = true
      someUIView.bottomAnchor.constraint(equalTo: self.safeAreaLayoutGuide.topAnchor, constant: 63).isActive = true

    }

ViewController

class ViewController: UIViewController {
     someTableView = UITableView()       

     someUIView: SomeUIView { return self.view as! SomeUIView}

     ovveride func viewDidLoad() {
     super.viewDidLoad()
     self.view = SomeUIView(frame: UIScreen.main.bounds)
     setupTableView()
     }

     func setupTableView() {
       view.addSubview(searchResultsTableView)
       someTableView.contentInset = UIEdgeInsets(top: 70, left: 0, bottom: 0, right: 0)
       someTableView.translatesAutoresizingMaskIntoConstraints = false
       someTableView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 40).isActive = true
       someTableView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor, constant: 0).isActive = true
       someTableView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor).isActive = true
       someTableView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor).isActive = true
       someTableView.register(SomeCell.self, forCellReuseIdentifier: "cell")
}

我想念什么?

1 个答案:

答案 0 :(得分:0)

问题是这一行:

self.view = SomeUIView(frame: UIScreen.main.bounds)

那是非法的。您无法在viewDidLoad中更改视图控制器的主视图。