如何从“自定义”表格单元格(UITableViewCell)显示自定义大小(使用preferredContentSize)弹出窗口

时间:2016-09-28 17:46:04

标签: ios uitableview uipopovercontroller

摘要:
我可以通过自定义表格单元格中的按钮操作获得全屏幕弹出窗口但不是自定义大小的弹出窗口。当按钮不在自定义单元格内时,我能够正常工作。在调试时,似乎当按钮在自定义表格单元格内时,没有调用popoverPresentationController的委托对象,这是调整弹出窗口大小的对象。

详细信息:

以下是当按钮在UITableViewCell之外时有效的代码:

import UIKit
import AVKit
import CoreGraphics

class ViewController: UIViewController, UIPopoverPresentationControllerDelegate {

func adaptivePresentationStyle(for controller: UIPresentationController) -> UIModalPresentationStyle {

    // This delegate function ensures that popup size is constrained to
   // what's specified in preferredContentSize earlier.
    return UIModalPresentationStyle.none
}

// This is triggered by a button in UIViewController.. nothing to do
// with the UITable here. This implementation works.

@IBAction func buttonpressed(_ sender: AnyObject) {
    let sb = self.storyboard
    let vc: AnyObject! = sb!.instantiateViewController(withIdentifier: "popover")
    let pc = vc as! PopOverWindow

    pc.modalPresentationStyle = UIModalPresentationStyle.popover
    pc.preferredContentSize = CGSize(width: 100, height: 100)

    // Needs to be called BEFORE we access      
    // popoverPresentationController below. Counter-intuitive but this
    // is how apple docs recommend it 
    self.present(pc, animated: true, completion: nil)

    let popover = pc.popoverPresentationController
    popover?.permittedArrowDirections = UIPopoverArrowDirection.any
    popover?.delegate = self 

    let button = sender as! UIButton

    // Need both statements below so that popover knows where to be
    // tethered
    popover?.sourceView = sender as? UIView

    // the position of the popover where it's showed
    // do not use buttons.frame
    popover?.sourceRect = button.bounds

}

这里的重要功能是adaptivePresentationStyle(),它确保弹出窗口不是全屏。

现在,让我们在自定义UITableViewCell中尝试相同的操作。这里最大的变化是UITableViewCell不是UIViewController的后代

@IBAction func infoButtonTouchDown(sender: AnyObject) {

    let sb = UIStoryboard(name: "Main", bundle: NSBundle.mainBundle())
    let vc: AnyObject! = sb.instantiateViewControllerWithIdentifier("popover")

    let pc = vc as! PopOverWindow        
    pc.modalPresentationStyle = .Popover
    pc.preferredContentSize = CGSizeMake(50,100)

    // Custom View Cell is not UIViewController so I need to following
    // to be able to call presentViewController later
    // I could have also done
    //self.window!.rootViewController?.presentViewController(..)
    // but the results were the same
    let t = self.window?.rootViewController as! UITabBarController
    let n = t.selectedViewController as! UINavigationController
    let m = n.visibleViewController as! MyTableViewController

    // Important: this needs to be called BEFORE we access
    // pc.popoverPresentationController object. Apple Docs say it.
    m.presentViewController(pc, animated: true, completion: nil)

    let popover = pc.popoverPresentationController
    popover?.permittedArrowDirections = UIPopoverArrowDirection.Any

    // I tried doing popover?.delegate = d to make the underlying table
    // as the delegate object but the results were same
    popover?.delegate = self

    let viewForSource = sender as! UIView
    popover?.sourceView = m.view

    // the position of the popover where it's showed
    // just some random position in view.
    popover?.sourceRect = CGRectMake(100,100,1,1) 
}

上面代码中的问题是代理函数adaptivePresentationStyle()没有被调用,因此我最终得到一个全屏弹出而不是半屏。

非常感谢任何帮助!

0 个答案:

没有答案