gpt4 book ai didi

ios - 隐藏导航栏而不移动scrollView

转载 作者:搜寻专家 更新时间:2023-10-31 22:32:39 26 4
gpt4 key购买 nike

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

image 1

请看这里的图片和条形图。

image 2

在这里,我只是点击了 View ,条形图被隐藏了,但正如您所看到的,我的 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)

}

知道如何在不影响其他 View 的情况下隐藏和显示栏吗?

最佳答案

问题是您需要将 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

你可以在project 37中看到它

希望对您有所帮助。

关于ios - 隐藏导航栏而不移动scrollView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36645122/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com