2016-04-15 108 views
2

我有一個viewController,其中顯示圖像添加縮放功能我添加scrollView在viewController和ScrollView裏面我添加ImageView一切工作良好期望的一件事,是隱藏,並顯示酒吧(導航欄+標籤欄)上自來水,但隱藏在他們我的ImageView招式上攻看到下面的圖片隱藏導航欄不移動scrollView

image 1

在這裏看到的圖像和酒吧。

image 2

在這裏,我只是拍了拍觀點和酒吧得到隱藏的,但你可以看到我的ImageView也從以前的地方移動,這是我要解決我不想動我的東西ImageView的。

這是怎麼了隱藏我的導航欄:

 func tabBarIsVisible() ->Bool { 
    return self.tabBarController?.tabBar.frame.origin.y < CGRectGetMaxY(self.view.frame) 
} 


func toggle(sender: AnyObject) { 
    navigationController?.setNavigationBarHidden(navigationController?.navigationBarHidden == false, animated: true) 
     setTabBarVisible(!tabBarIsVisible(), animated: true) 

} 

任何想法如何,我可以隱藏和顯示條在不影響我其他的意見?

+1

因此,你的形象一直在視圖的中心?當酒吧消失時,您是否嘗試再次設置其中心? – Khuong

+0

好吧,重新設置在中心是有道理的,但我們沒有其他選擇嗎?你是否還記得在'photos'應用程序中隱藏的酒吧過渡,它只是消失,而不是向上移動任何想法如何做到這一點? –

+1

我沒有任何iPhone,所以我不確切知道照片應用的功能。但是你可以添加淡入淡出動畫:-)。 – Khuong

回答

2

問題是您需要將您的imageView的約束條件設置爲您的superView而不是TopLayoutGuideBottomLayoutGuide

像這樣:

enter image description here

然後,你可以做這樣的事情動畫,使之smootly:

import UIKit 

class ViewController: UIViewController { 

    @IBOutlet var imageView: UIImageView! 
    var barIsHidden = false 
    var navigationBarHeight: CGFloat = 0 
    var tabBarHeight: CGFloat = 0 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     let tapGesture = UITapGestureRecognizer(target: self, action: #selector(ViewController.hideAndShowBar)) 
     view.addGestureRecognizer(tapGesture) 
     navigationBarHeight = (self.navigationController?.navigationBar.frame.size.height)! 
     tabBarHeight = (self.tabBarController?.tabBar.frame.size.height)! 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
    } 

    func hideAndShowBar() { 
     print("tap!!") 
     if barIsHidden == false { 
      UIView.animateWithDuration(0.5, delay: 0.0, options: .CurveEaseOut, animations: { 
       // fade animation 
       self.navigationController?.navigationBar.alpha = 0.0 
       self.tabBarController?.tabBar.alpha = 0.0 
       // set height animation 
       self.navigationController?.navigationBar.frame.size.height = 0.0 
       self.tabBarController?.tabBar.frame.size.height = 0.0 
       }, completion: { (_) in 
        self.barIsHidden = true 
      }) 
     } else { 
      UIView.animateWithDuration(0.5, delay: 0.0, options: .CurveEaseOut, animations: { 
       // fade animation 
       self.navigationController?.navigationBar.alpha = 1.0 
       self.tabBarController?.tabBar.alpha = 1.0 
       // set height animation 
       self.navigationController?.navigationBar.frame.size.height = self.navigationBarHeight 
       self.tabBarController?.tabBar.frame.size.height = self.tabBarHeight 
       }, completion: { (_) in 
        self.barIsHidden = false 
      }) 
     } 
    } 

} 

下面是結果:

enter image description here

我有cr eated一個例子項目您的位置:https://github.com/khuong291/Swift_Example_Series

您可以在項目37

希望這將幫助你看到它。