我正在尝试在iPhone上实现一个类似于iPad的模式演示的popovercontroller
。我只能在使用storyboard时生成UI,我更喜欢以编程方式完全构建控制器。 SO和Google上的所有演示似乎都使用了故事板。我可以看到从我的自定义UIViewController
打印到日志的方法,但模态弹出窗口中没有任何内容。 Showing Popover view
func handlePop(sender: UIButton) {
//let popController:UIViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "unique") as UIViewController //only way I generate UI in popoverview
let popController = MyCustomViewController() //would like to use
//positioning
let placementView = UIView()
view.addSubview(placementView)
view.addConstraintsWithFormat("H:|[v0]|", views: placementView)
view.addConstraintsWithFormat("H:|-64-[v0(200)]", views: placementView)
popController.modalPresentationStyle = UIModalPresentationStyle.popover
if let poppingVC = popController.popoverPresentationController {
let popover: UIPopoverPresentationController = poppingVC
popover.sourceView = placementView
popover.permittedArrowDirections = .up
popover.sourceRect = CGRect(x: view.frame.width / 2, y: -100, width: 0, height: 100)
popover.delegate = self
self.present(popController, animated: true, completion:nil)
}
}
func dismissView() {
self.dismiss(animated: true, completion: nil)
}
func adaptivePresentationStyle(for controller: UIPresentationController) -> UIModalPresentationStyle {
return UIModalPresentationStyle.none
}
我错过了什么让popovercontroller
空白?
class RegionSelectionController: UIViewController, UIPopoverPresentationControllerDelegate, UITableViewDelegate, UITableViewDataSource {
let mainLabel = UILabel()
let cellId = "cellId"
let footerId = "footerId"
var keys = [String]()
let tableView = UITableView()
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
self.view.addSubview(tableView)
tableView.register(RegionFilterCell.self, forCellReuseIdentifier: cellId)
tableView.separatorStyle = .none
tableView.delegate = self
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
print(keys.count)
return keys.count
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as! RegionFilterCell
let regionName = keys[indexPath.row]
cell.regionLabel.text = regionName
return cell
}
func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
let footerCell = RegionRequestNewLocationCell()
return footerCell
}
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return 100
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print(indexPath.row, keys[indexPath.row])
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
ref.child("Region").observeSingleEvent(of: .childAdded, with: { (snap) in
if snap.exists() {
let key: String = snap.key as String
self.keys.append(key)
}
DispatchQueue.main.async ( execute: {
self.tableView.reloadData()
})
})
}
}
class RegionFilterCell: BaseTableCell {
let regionLabel: UILabel = {
let label = UILabel()
label.font = UIFont(name: "SFUIText-Medium", size: 16)
label.text = "UMD"
return label
}()
let separatorBar: UIView = {
let view = UIView()
view.backgroundColor = UIColor.rgb(151, green: 151, blue: 151, alpha: 0.3)
return view
}()
override func setupViews() {
super.setupViews()
addSubview(regionLabel)
addSubview(separatorBar)
//addConstraintsWithFormat("H:[v0(100)]", views: regionLabel)
addConstraint(NSLayoutConstraint(item: regionLabel, attribute: .centerX, relatedBy: .equal, toItem: self, attribute: .centerX, multiplier: 1, constant: 0))
addConstraintsWithFormat("V:|[v0]|", views: regionLabel)
addConstraintsWithFormat("H:|[v0]|", views: separatorBar)
addConstraintsWithFormat("V:[v0(0.5)]|", views: separatorBar)
}
}
答案 0 :(得分:0)
根据您显示的内容,您永远不会在ViewController中为您的内容设置框架(在本例中为您的UITableView
)。如果您没有为视图设置框架,则不会在屏幕上显示。您需要使用CGRect
指定原点和大小,或使用带NSLayoutConstraints
的自动布局。最简单的方法就是一个框架。所以在viewDidLoad
中,假设你想让tableView占用整个视图:
tableView.frame = CGRect(x: 0, y: 0, width: view.bounds.width, height: view.bounds.height)
此外,您需要确保您正确实例化正确的VC。我不知道您是否仅使用名称MyCustomViewController
作为示例,但您在代码中显示的VC名为RegionSelectionController