2016-02-22 88 views
0

我想基於某些設置更改我的Modal的preferredContentSize。動態更改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() 
} 

任何想法:

不適與正常調整呢?提前致謝。

回答

0

你是否嘗試設置tableView的footerview非常小?

夫特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() 
}