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

时间:2017-12-02 23:14:45

标签: ios swift uiimageview uiimage didselectrowatindexpath

当前缩略图正在UITableViewCell中显示,并且在点击单元格时,我希望图像显示在前景中,右上角有一个十字/ 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)

图像仍无法显示。计算正在进行代码的这一部分,因为我能够调试并逐步执行这部分代码。但屏幕上没有任何内容。我使用的是zPosition AND bringSubview(toFront:),而且这两个似乎都不符合我的要求。任何帮助将不胜感激。谢谢。

1 个答案:

答案 0 :(得分:1)

这是一个Demo表格视图。点击表格视图单元格,一个视图通过关闭按钮加速。按下关闭按钮后,弹出视图将关闭。

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: {
        })
    }

}

}

A table view

A simple view for Image