如何在点击 tableViewCell 时将 UIImageView 置于前台?

How to bring UIImageView to foreground upon tapping a tableViewCell?

目前缩略图显示在 UITableViewCell 中,点击一个单元格后,我希望图像显示在前景中,右上角有一个 cross/X 按钮以关闭图像并显示 tableView。我在 didSelectRow 中有以下代码:

    let hoverImage = UIImageView()
    hoverImage.image = UIImage(named: "splashpt")
    hoverImage.contentMode = .scaleAspectFit
    self.view.addSubview(hoverImage)
    hoverImage.center = self.view.center
    hoverImage.layer.zPosition = 5
    self.view.bringSubview(toFront: hoverImage)

图像仍然没有显示。计算命中了这部分代码,因为我能够调试并单步执行这部分代码。但是屏幕上什么也没有显示。我正在使用 zPositionbringSubview(toFront:),但似乎都不能满足我的要求。任何帮助将不胜感激。谢谢。

这是一个演示 table view.On 点击 table 视图单元格弹出一个带有关闭按钮的视图。并在按下关闭按钮时关闭弹出视图。

import UIKit

class ViewController: UITableViewController  {

override func viewDidLoad() {
    super.viewDidLoad()


    // Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 5
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell:UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("cell")! as UITableViewCell

    cell.textLabel?.text = "Happy"
    return cell
}

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    print("Cell\(indexPath.row) is selected")



    let storyBoard = UIStoryboard(name: "Main", bundle: nil)
    let popoverVC = storyBoard.instantiateViewControllerWithIdentifier("PopViewController") as! PopViewController
    popoverVC.delegate = parentViewController as? InfoViewDelegate
    let nav = UINavigationController(rootViewController: popoverVC)
    nav.modalPresentationStyle = UIModalPresentationStyle.Popover
    nav.navigationBar.hidden = true
    self.presentViewController(nav, animated: true)
    {

    }
    popoverVC.passingViewController = self

}
}

这是一个 PopUpViewController:

 import UIKit
 protocol InfoViewDelegate: class
{
func infoViewClicked(tag: Int)
}

class PopViewController :UIViewController
{

@IBOutlet weak var btnClose: UIButton!
var delegate = InfoViewDelegate?()
var passingViewController: UIViewController!
override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

override func viewWillAppear(animated: Bool) {
}

override func viewDidDisappear(animated: Bool) {
    self.presentingViewController?.dismissViewControllerAnimated(true
        , completion: {
    })
}
/*
 // MARK: - Navigation

 // In a storyboard-based application, you will often want to do a little preparation before navigation
 override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
 // Get the new view controller using segue.destinationViewController.
 // Pass the selected object to the new view controller.
 }
 */
@IBAction func btnClicked(sender: UIButton) {
    if(sender == self.btnClose)
    {
        self.delegate?.infoViewClicked(1)
        self.presentingViewController?.dismissViewControllerAnimated(true
            , completion: {
        })
    }

}

}