Drop down list ios swift

点点圈 提交于 2019-12-23 00:22:09

问题


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


回答1:


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
}


来源:https://stackoverflow.com/questions/44688498/drop-down-list-ios-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!