behrad
behrad

Reputation: 646

how to popup tableView from bottom of screen?

in my project I want that after presenting viewController, tableView popup from bottom of screen until showing all the content of tableView. the UITableViewCell height is dynamic and I'm using swift language and NSLayoutConstraint in my project. how can I do this? it's only showing some part of tableView.

this is my codes:

class myViewController: UIViewController {

var tableView: UITableView!
var tableViewTopLayoutConstraint, tableViewHeightLayoutConstraint: NSLayoutConstraint!
private var isFirstTime: Bool = true

override func viewDidLayoutSubviews() {
    
    print("this is tableView frame Height:\(tableView.frame.height)")
    print("this is tableView content Height:\(tableView.contentSize.height)")
    
    if tableView.contentSize.height != 0 && isFirstTime {
        
        tableViewHeightLayoutConstraint.constant = tableView.contentSize.height
        showContainerView(-tableView.contentSize.height)
    }
}

override func viewDidLoad() {
    super.viewDidLoad()
    
    view.backgroundColor = .gray

    tableView = UITableView()
    tableView.layer.maskedCorners = [.layerMinXMinYCorner, .layerMaxXMinYCorner]
    tableView.layer.cornerRadius = 20
    tableView.register(SomeUITableViewCell.self, forCellReuseIdentifier: SomeUITableViewCell.identifier)
    
    tableView.rowHeight = UITableView.automaticDimension
    
    tableView.separatorStyle = .none
    tableView.backgroundColor = .clear
    tableView.tableFooterView  = nil
    tableView.isScrollEnabled = false
    tableView.delegate = self
    tableView.dataSource = self
    tableView.translatesAutoresizingMaskIntoConstraints = false
    view.addSubview(tableView)
    
    tableViewTopLayoutConstraint = tableView.topAnchor.constraint(equalTo: view.bottomAnchor, constant: 0)
    tableViewTopLayoutConstraint.isActive = true
    
    tableViewHeightLayoutConstraint = tableView.heightAnchor.constraint(equalToConstant: 0)
    tableViewHeightLayoutConstraint.isActive = true
    
    NSLayoutConstraint.activate([
        tableView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
        tableView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
    ])
}

private func showContainerView(_ viewHeight: CGFloat) {
    
    isFirstTime = false
    tableViewTopLayoutConstraint.constant = viewHeight
    UIView.animate(withDuration: 0.5) { [weak self] in
        self?.view.layoutIfNeeded()
    }
}
}

extension myViewController: UITableViewDelegate, UITableViewDataSource {

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

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    
    let cell = tableView.dequeueReusableCell(withIdentifier: SomeUITableViewCell.identifier, for: indexPath) as! SomeUITableViewCell
    cell.setCell()
    return cell
}
}

class SomeUITableViewCell: UITableViewCell {

static let identifier = "SomeUITableViewCellId"

private var cardOrDepositNumberLabel: UILabel!
private var cardOrDepositOwnerLabel: UILabel!

override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
    super.init(style: style, reuseIdentifier: reuseIdentifier)
    
    selectionStyle = .none
    
    cardOrDepositNumberLabel = UILabel()
    cardOrDepositNumberLabel.numberOfLines = 0
    cardOrDepositNumberLabel.backgroundColor = .red
    cardOrDepositNumberLabel.textAlignment = .right
    cardOrDepositNumberLabel.translatesAutoresizingMaskIntoConstraints = false
    addSubview(cardOrDepositNumberLabel)
    
    NSLayoutConstraint.activate([
        cardOrDepositNumberLabel.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 10),
        cardOrDepositNumberLabel.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -10),
        cardOrDepositNumberLabel.topAnchor.constraint(equalTo: topAnchor),
    ])
    
    cardOrDepositOwnerLabel = UILabel()
    cardOrDepositOwnerLabel.numberOfLines = 0
    cardOrDepositOwnerLabel.textAlignment = .right
    cardOrDepositOwnerLabel.backgroundColor = .blue
    cardOrDepositOwnerLabel.translatesAutoresizingMaskIntoConstraints = false
    addSubview(cardOrDepositOwnerLabel)

    NSLayoutConstraint.activate([
        cardOrDepositOwnerLabel.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 10),
        cardOrDepositOwnerLabel.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -10),
        cardOrDepositOwnerLabel.topAnchor.constraint(equalTo: cardOrDepositNumberLabel.bottomAnchor),
        cardOrDepositOwnerLabel.bottomAnchor.constraint(equalTo: bottomAnchor)
    ])
}

required init?(coder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

func setCell() {
    
    cardOrDepositNumberLabel.text = "some data some data some data"
    cardOrDepositOwnerLabel.text = "some data some data some data some data some data some data some data some data some data some data"
}
}

Upvotes: 0

Views: 693

Answers (1)

Shehata Gamal
Shehata Gamal

Reputation: 100533

Problem is here

tableViewHeightLayoutConstraint = tableView.heightAnchor.constraint(equalToConstant: 300)

set an intial no zero value and do what is inside viewDidLayoutSubviews in viewDidAppear preferably after a delay to give some time to the table to calculate it's actualy contentSize

Upvotes: 1

Related Questions