如何在swift中单击UitableView单元格时创建动态大小的弹出窗口?
我应该使用任何工具提示框架吗?或者只是使用苹果PopOverViewController功能?
使用以下代码在iPad中正常工作。但在iPhone中它显示为全屏视图
下面的代码工作正常,是我得到的解决方案UIPopoverPresentationController on iPhone doesn't produce popover
by @tsik
class CommonViewController: UIViewController, UIPopoverPresentationControllerDelegate{
func adaptivePresentationStyle(
for controller: UIPresentationController,
traitCollection: UITraitCollection)
-> UIModalPresentationStyle {
return .none
}
func showPopover(){
let storyboard = UIStoryboard(name: "Pickers", bundle: nil)
let myViewController = UIViewController()
myViewController.preferredContentSize = CGSize(width: 320, height: 200)
myViewController.modalPresentationStyle = .popover
let popOver = myViewController.popoverPresentationController
popOver?.delegate = self
self.present(myViewController, animated: true, completion: nil)
popOver?.permittedArrowDirections = .init(rawValue: 0)
popOver?.sourceView = self.view
let rect = CGRect(
origin: CGPoint(x: self.view.frame.width/2, y: self.view.frame.height/2),
size: CGSize(width: 1, height: 1)
)
popOver?.sourceRect = rect
}
答案 0 :(得分:1)
请你检查一下这个代码,在我的情况下这在iphone中运行正常。
let popoverContent = self.storyboard!.instantiateViewController(withIdentifier: "PopOverViewController")
let ktagForLabel = 100
let popOverWidth = UIScreen.main.bounds.width - 40
if let label = popoverContent.view.viewWithTag(ktagForLabel) as? UILabel {
let descriptionText = self.subcategoryDetailViewModel.displayServiceDescriptionForPopOver(forIndex: sender.tag/10)
label.text = descriptionText
var estimatedTextHeight = descriptionText.heightWithConstrainedWidth(width: popOverWidth, font: UIFont.systemFont(ofSize: 15)) + 16
let nav = UINavigationController(rootViewController: popoverContent)
nav.isNavigationBarHidden = true
nav.modalPresentationStyle = UIModalPresentationStyle.popover
let popover = nav.popoverPresentationController!
estimatedTextHeight = estimatedTextHeight > 150 ? 150: estimatedTextHeight
popoverContent.preferredContentSize = CGSize(width: popOverWidth, height: estimatedTextHeight)
popover.delegate = self
popover.permittedArrowDirections = .up
popover.sourceView = sender
popover.sourceRect = sender.bounds
self.present(nav, animated: true, completion: nil)
}