隐藏导航栏而不移动scrollView
Hide navigation bar without moving scrollView
我有一个 viewController,我在其中显示用于添加缩放功能的图像 我在 viewController 中添加了 scrollView,在 ScrollView 内部我添加了 ImageView 一切正常,我希望有一件事,我正在隐藏,并在点击时显示栏(导航栏 + 标签栏),但是当隐藏它们时,我的 imageView 向上移动,请参见下图
请看这里的图片和条形图。
在这里,我只是点击了视图,条形图被隐藏了,但是正如您所看到的,我的 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)
}
知道如何在不影响其他视图的情况下隐藏和显示栏吗?
问题是您需要将 imageView
的约束设置为 superView
,而不是 TopLayoutGuide
或 BottomLayoutGuide
。
像这样:
然后你可以做这样的事情来让动画变得流畅:
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
})
}
}
}
结果如下:
我在以下位置为您创建了一个示例项目:https://github.com/khuong291/Swift_Example_Series
你可以在项目37
看到它
希望对您有所帮助。
我有一个 viewController,我在其中显示用于添加缩放功能的图像 我在 viewController 中添加了 scrollView,在 ScrollView 内部我添加了 ImageView 一切正常,我希望有一件事,我正在隐藏,并在点击时显示栏(导航栏 + 标签栏),但是当隐藏它们时,我的 imageView 向上移动,请参见下图
请看这里的图片和条形图。
在这里,我只是点击了视图,条形图被隐藏了,但是正如您所看到的,我的 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)
}
知道如何在不影响其他视图的情况下隐藏和显示栏吗?
问题是您需要将 imageView
的约束设置为 superView
,而不是 TopLayoutGuide
或 BottomLayoutGuide
。
像这样:
然后你可以做这样的事情来让动画变得流畅:
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
})
}
}
}
结果如下:
我在以下位置为您创建了一个示例项目:https://github.com/khuong291/Swift_Example_Series
你可以在项目37
看到它希望对您有所帮助。