2016-04-15 107 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

希望这将帮助你看到它。