在 iphone 上创建仅包含自定义 viewcontroller class 的弹出概览
Creating a popoverview with only custom viewcontroller class on iphone
我正在尝试在 iPhone 上实现一个 popovercontroller
,它具有类似于 iPad 的模态表示。我只能在使用故事板时生成 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)
}
}
从您所展示的内容来看,您永远不会在正在展示的 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
我正在尝试在 iPhone 上实现一个 popovercontroller
,它具有类似于 iPad 的模态表示。我只能在使用故事板时生成 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)
}
}
从您所展示的内容来看,您永远不会在正在展示的 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