Drop down list ios swift

2019-05-22 20:48发布

I want to have a small UItableView that popup when clicked and shows some numbers in the list.

I tried to use popoverPresentationController but it appears full screen for iOS(iPhone) devices.

below is the code for same -

let filterVC =  TableViewController(nibName: "TableViewController", bundle: nil)
              filterVC.preferredContentSize = CGSize(width: 300, height: 200)

    filterVC.modalPresentationStyle = UIModalPresentationStyle.popover

    present(filterVC, animated: true, completion: nil)
    let popoverPresentationController = filterVC.popoverPresentationController

    if let pop = filterVC.popoverPresentationController {
        pop.delegate = self
    }

       popoverPresentationController?.sourceView = sender as? UIView
    popoverPresentationController?.sourceRect = sender.frame

//-------

with below method also

  func adaptivePresentationStyle(for controller: UIPresentationController) -> UIModalPresentationStyle {
    // Return no adaptive presentation style, use default presentation behaviour
    return .none
}

//----- Any hint in right direction would be highly appreciated. working sample would be greatly helpful

What I am trying to achieve as below

dropdwon 1

dropdown 2

1条回答
Summer. ? 凉城
2楼-- · 2019-05-22 21:08

UPDATE

There is a useful library you may want to give a try.


It's because your pop.delegate was assigned after you present the filterVC.

Move this

if let pop = filterVC.popoverPresentationController {
    pop.delegate = self
    pop.sourceView = sender
    pop.sourceRect = sender.bounds
}
present(filterVC, animated: true, completion: nil)

to the init of your filterVC should do the trick. Btw, I didn't see anywhere you have assigned sourceView and sourceRect for your popoverPresentationController. Moving pop.delegate = self to this part should be appropriate. Something like

init(for sender: UIView)) {
    super.init(nibName: nil, bundle: nil)

    modalPresentationStyle = .popover
    guard let pop = popoverPresentationController else { return }
    pop.sourceView = sender
    pop.sourceRect = sender.bounds
    pop.delegate = self
}
查看更多
登录 后发表回答