as diu
as diu

Reputation: 1090

How to bring UIImageView to foreground upon tapping a tableViewCell?

currently thumbnails are being shown in UITableViewCell and upon tapping a cell, I want the image to be shown in foreground with a cross/X button on right top to dismiss the image and show tableView. I have the following code in 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)

Still the image doesn't show up. The computation is hitting this section of the code because I'm able to debug and step through this part of the code. But nothing shows up on screen. I'm using the zPosition AND bringSubview(toFront:) and neither of the seem to work for my requirement. Any help would be greatly appreciated. Thank you.

Upvotes: 0

Views: 60

Answers (1)

Niroj
Niroj

Reputation: 1114

This is a Demo table view.On tapping the table view cells a view is poped up with close button. And on pressing the close button the pop up view is closed.

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

}
}

This is a 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

Upvotes: 1

Related Questions