动态更改preferredContentSize?

时间:2016-02-22 00:37:48

标签: ios swift

我想根据一些设置更改我的模态的preferredContentSize。

例如:有一个开关,如果开关关闭,我想隐藏一些单元格。多数民众赞成,但我也想调整模式。

通过以下方式正常调整大小:

var contentSize = CGSizeMake(560, 562)

override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)
    self.preferredContentSize = contentSize
}

@IBAction func photoSwitchPressed(sender: UISwitch) {
    if sender.on == true {
        photos = true
    } else {
       photos = false
        contentSize = CGSizeMake(560, 462) // resize?
        self.viewWillAppear(true) // how to force resize?
    }
    tableView.reloadData()
}

有什么想法吗?提前谢谢。

1 个答案:

答案 0 :(得分:0)

您是否尝试将tableView的页脚视图设置得非常小?

Swift 3

var contentSize = CGSize(width: 560, height: 562)
var photos = false

override func viewDidLoad() {
    super.viewDidLoad()
    tableView.tableFooterView = UIView(frame: CGRect(x: 0, y: 0, width: 1, height: 1))
}

override func viewWillAppear(_ animated: Bool) {
    preferredContentSize = contentSize
}

@IBAction func photoSwitchPressed(_ sender: UISwitch) {
    if sender.isOn == true {
        photos = true
    } else {
        photos = false
        contentSize = CGSize(width: 560, height: 462)

    }
    tableView.reloadData()
}
相关问题