how to make UIScrollView's dynamic height

35 views Asked by At

I made my view like this. only for a vertical scroll

enter image description here

view Components: ScrollView, contentView(UIView), headerView, tableView

I placed contentView on ScrollView , and headerView and TableView on ContentView.

Question: how to make scrollView's dynamic height.

actually contentView's height must have dynamic height right?

class BusStopViewController: UIViewController {
    
    let viewModel = ViewModel()
    let disposeBag = DisposeBag()
    
    private let headerView: BusStopInfoHeaderView = BusStopInfoHeaderView()
    
    private var scrollView: UIScrollView = UIScrollView()
    
    private let tableView: UITableView = {
        let tv = UITableView(frame: .zero, style: .insetGrouped)
        tv.register(TableCell.self,
                    forCellReuseIdentifier: TableCell.identifier)
        tv.isScrollEnabled = false
        return tv
    }()
    
    let dataSource = RxTableViewSectionedReloadDataSource<DataSection> { dataSource, tableView, indexPath, item in
        guard let cell = tableView.dequeueReusableCell(withIdentifier: TableCell.identifier, for: indexPath) as? TableCell else { return UITableViewCell() }
        cell.selectionStyle = .none
        cell.bind(with: item)
        
//        tableView.frame.size = tableView.contentSize
//        print("tableView의 frame.size.height - \(tableView.frame.size.height)")
        
        return cell
    } titleForHeaderInSection: { dataSource, indexPath in
        return dataSource.sectionModels[indexPath].header
    }
    
    private let totalView = UIView()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.backgroundColor = .yellow
        scrollView.backgroundColor = .red
        configureUI()
        
        tableView.rx.setDelegate(self)
            .disposed(by: disposeBag)
        
        bind()
        
        tableView.rx.itemSelected
            .subscribe { index in
                print("\(index) 선택됨")
            }
            .disposed(by: disposeBag)
        
    }
    
    func bind() {
        Observable.just(viewModel.sections)
            .bind(to: tableView.rx.items(dataSource: dataSource))
            .disposed(by: disposeBag)
    }
    
    func configureUI() {
        view.addSubview(scrollView)
//        scrollView.updateContentSize()
        
        print("\(scrollView.contentSize.height)")
        
        [scrollView, totalView, headerView, tableView]
            .forEach { components in
                components.translatesAutoresizingMaskIntoConstraints = false
            }
        
        [headerView, tableView]
            .forEach { components in
                totalView.addSubview(components)
            }
        
        scrollView.addSubview(totalView)
        
        NSLayoutConstraint.activate([
            scrollView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
            scrollView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor),
            scrollView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor),
            scrollView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor),
            
            totalView.widthAnchor.constraint(equalTo: scrollView.widthAnchor),
            totalView.heightAnchor.constraint(greaterThanOrEqualToConstant: view.bounds.height),
            
            totalView.topAnchor.constraint(equalTo: scrollView.contentLayoutGuide.topAnchor),
            totalView.leadingAnchor.constraint(equalTo: scrollView.contentLayoutGuide.leadingAnchor),
            totalView.trailingAnchor.constraint(equalTo: scrollView.contentLayoutGuide.trailingAnchor),
            totalView.bottomAnchor.constraint(equalTo: scrollView.contentLayoutGuide.bottomAnchor),
            
            headerView.topAnchor.constraint(equalTo: scrollView.topAnchor),
            headerView.leadingAnchor.constraint(equalTo: scrollView.leadingAnchor),
            headerView.trailingAnchor.constraint(equalTo: scrollView.trailingAnchor),
            headerView.widthAnchor.constraint(equalTo: scrollView.widthAnchor),
            
            tableView.topAnchor.constraint(equalTo: headerView.bottomAnchor),
            tableView.leadingAnchor.constraint(equalTo: totalView.leadingAnchor),
            tableView.widthAnchor.constraint(equalTo: totalView.widthAnchor),
            tableView.heightAnchor.constraint(equalTo: totalView.heightAnchor),
        ])
    }
    
}

extension BusStopViewController: UITableViewDelegate {
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 55
    }
    
}
0

There are 0 answers