Изменить высоту HeaderView внутри tableViewSection с помощью Animation + UIStackView - PullRequest
1 голос
/ 22 июня 2019

У меня есть один Tableview.внутри табличного представления при вставке стека, который содержит 2 view(Blue and Gray) с FillEqually.Теперь я сталкиваюсь с проблемой, пока скрываю один вид (синий).другой вид (серый) имеет место и делает его больше.высота headerview должна быть изменена после скрытия другого вида.я дал UITableView.automaticDimension.Ниже приведен пример кода.

class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {
    @IBOutlet weak var tblView: UITableView!
    override func viewDidLoad() {
        super.viewDidLoad()
        tblView.delegate = self
        tblView.dataSource = self
        tblView.estimatedSectionHeaderHeight = UITableView.automaticDimension
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)

        cell.textLabel?.text = "Section \(indexPath.section) Row \(indexPath.row)"

        return cell
    }

    func numberOfSections(in tableView: UITableView) -> Int {
        return 10
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 2
    }



    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {

        let headerView:headerView = (Bundle.main.loadNibNamed("headerView", owner: self, options: nil)?.first as? headerView)!

        DispatchQueue.main.asyncAfter(deadline: .now() + 1.5) {
            UIView.animate(withDuration: 1, animations: {
                self.tblView.beginUpdates()
                headerView.view1.isHidden = true
                headerView.stackview.setNeedsLayout()
                headerView.stackview.layoutIfNeeded()
                self.view.setNeedsLayout()
                self.view.layoutIfNeeded()
                self.tblView.endUpdates()
            })

        }
         return headerView
    }

    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return UITableView.automaticDimension
    }
}

Вид заголовка - это xib с кодом ниже

class headerView: UIView {
    //initWithFrame to init view from code
    @IBOutlet weak var view2: UIView!
    @IBOutlet weak var stackview: UIStackView!
    @IBOutlet weak var view1: UIView!
    override init(frame: CGRect) {
        super.init(frame: frame)

    }

    //initWithCode to init view from xib or storyboard
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)

    }

    //common func to init our view
    private func setupView() {
        backgroundColor = .red
    }
}

Ограничение, данное в HeaderView.Xib

enter image description here

Что я хочу здесь. например, высота view1 равна 30, view2 равна 30, тогда общая высота равна 60 стека и заголовка.так что после спрятать вид1.общая высота заголовка и стека составляет только 30.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...