0

enter image description here

How to create a popover of dynamic size on clicking UitableView cell in swift?

Should i use any tooltip frameworks? or just use apples PopOverViewController functionality?

It works fine in iPad with the below code. but in iphone it appears as a full screen view

Below code works fine and is the solution i got 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
}
j.krissa
  • 223
  • 5
  • 21

1 Answers1

1

could you please check this code this is working fine in iphones in my case.

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)
}
SuryaKantSharma
  • 1,113
  • 12
  • 27