隐藏导航栏而不移动scrollView

时间:2016-04-15 10:48:37

标签: ios swift uiscrollview uinavigationbar

我有一个viewController,其中显示了用于添加缩放功能的图像我在viewController中添加了scrollView,在ScrollView内部我添加了ImageView一切正常工作,我正在隐藏,并显示条形图(导航栏+选项卡)吧)点击但是当隐藏它们时我的imageView向上移动看到下面的图像

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 个答案:

答案 0 :(得分:3)

问题是您需要将imageView的约束设置为superView,而不是TopLayoutGuideBottomLayoutGuide

像这样:

enter image description here

然后你可以做这样的事情,用动画来实现它:

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

我已经为您创建了一个示例项目:https://github.com/khuong291/Swift_Example_Series

您可以在项目37

中看到它

希望这会对你有所帮助。

相关问题