TableViewController没有正确向上滚动

时间:2017-12-10 16:28:23

标签: ios swift

在我的应用程序中,用户点击按钮后,UITableViewController从屏幕底部出现,超过屏幕的2/3。该表填充了数据,但我无法向上滚动以查看其余数据。

import UIKit

class CityTableViewController: UITableViewController, UIViewControllerTransitioningDelegate {

    var cities: [String] = ["New York City", "Montreal", "San Francisco", "Paris", "Lisbon", "London", "Philadelphia", "Los Angeles", "Boston", "Atlanta"]

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)

        self.modalPresentationStyle = .custom
        self.transitioningDelegate = self
    }

    func presentationController(forPresented presented: UIViewController, presenting: UIViewController?, source: UIViewController) -> UIPresentationController? {

        return CityPop(presentedViewController: presented, presenting: presenting)
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        // Uncomment the following line to preserve selection between presentations
         self.clearsSelectionOnViewWillAppear = false

        // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
        // self.navigationItem.rightBarButtonItem = self.editButtonItem
        //automaticallyAdjustsScrollViewInsets = false
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // MARK: - Table view data source

    override func numberOfSections(in tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return cities.count
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "CityCell", for: indexPath)
        cell.textLabel?.text = cities[indexPath.row]
        cell.textLabel?.font = UIFont(name: "Goku", size: 33.9)
        cell.textLabel?.textAlignment = .center
        // Configure the cell...

        return cell
    }

}

class CityPop: UIPresentationController {

    override var frameOfPresentedViewInContainerView: CGRect {

        let containerFrame = self.containerView!.frame

        return CGRect(x: 0, y: containerFrame.height/3, width: containerFrame.width, height: containerFrame.height)
    }
}

enter image description here

有没有更好的方法来实现相同的效果,而不是使用我的TableView和UIPresentationController的子类?

1 个答案:

答案 0 :(得分:2)

这块是有问题的块。

return CGRect(x: 0, y: containerFrame.height/3, width: containerFrame.width, height: containerFrame.height)

只有当数据无法满足给定的内容大小时,系统才会显示滚动。这里实际上是为视图控制器提供全屏高度,足以显示所有内容城市名称。你需要在屏幕边界中正确地改变高度。

将此更改为

return CGRect(x: 0, y: containerFrame.height/3, width: containerFrame.width, height: 2*(containerFrame.height/3))