如何在点击图像时隐藏导航栏

时间:2017-05-07 07:48:51

标签: ios iphone swift uinavigationbar

点击我的imageView后,如何隐藏导航栏,一旦我的imageView被点击,导航栏就会弄乱全屏图像的视图,我想在点击图像时将其隐藏,并在图像重新出现后再次出现被解雇了。这是我的图像被点击的代码。

//expandImage




@IBAction func expand(_ sender: UITapGestureRecognizer) {
    let imageView = sender.view as! UIImageView
    let newImageView = UIImageView(image: imageView.image)
    newImageView.frame = self.view.frame
    newImageView.backgroundColor = .black
    newImageView.contentMode = .scaleAspectFit
    newImageView.isUserInteractionEnabled = true
    let tap = UITapGestureRecognizer(target: self, action: #selector(dismissFullscreen))
    newImageView.addGestureRecognizer(tap)
    self.view.addSubview(newImageView)
}

func dismissFullscreen(_ sender: UITapGestureRecognizer) {
    sender.view?.removeFromSuperview()
}

2 个答案:

答案 0 :(得分:1)

所以你可以这样做:

class ViewController: UIViewController {

    @IBOutlet weak var imageView: UIImageView!

    override func viewDidLoad() {
        super.viewDidLoad()
        let tapImageScrollView = UITapGestureRecognizer(target: self, action: #selector(imageTapped(_:)))
        imageView.isUserInteractionEnabled = true
        imageView.addGestureRecognizer(tapImageScrollView)
    }

    func imageTapped(_ sender: UIGestureRecognizer) {
        if self.navigationController?.navigationBar.isHidden == false {
            self.navigationController?.navigationBar.isHidden = true
        } else {
            self.navigationController?.navigationBar.isHidden = false
        }
    }
}

所以基本上在你的UITapGestureRecognizer添加imageView,在imageTapped函数中检查navigationBar是否未被隐藏,然后你要显示图像并隐藏navigationBar如果您再次点击imageView,则需要再次显示navigationBar

只需将imageTapped中的逻辑添加到dismissFullscreen函数中即可。

答案 1 :(得分:1)

将此添加到您的expand()方法:

self.navigationController?.setNavigationBarHidden(true, animated: true)

在dismissFullscreen()方法中:

self.navigationController?.setNavigationBarHidden(false, animated: true)

或者您可以创建新的ViewController,将图像传递给它(例如使用segue)并将其添加到新ViewController的viewDidLoad()中:

self.navigationController?.hidesBarsOnTap = true