2017-05-07 109 views
1

點擊我的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() 
} 

回答

1

添加到您的擴大()方法:

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

而且在dismissFullscreen()方法:

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

也可以創建新的視圖控制器,通過圖像將它(與SEGUE如)並將其添加到新的ViewController的viewDidLoad()中:

self.navigationController?.hidesBarsOnTap = true 
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 
     } 
    } 
} 

所以基本上添加UITapGestureRecognizerimageView,並在imageTapped功能,您檢查navigationBar沒有隱藏,那麼你要顯示的圖像並隱藏navigationBar,如果再次點擊imageView,您想再次顯示navigationBar

因此只需將imageTapped中的邏輯添加到您的dismissFullscreen函數中即可。