在iOS8中使用Swift点击时如何隐藏/显示选项卡栏
我试图用一个选项卡栏模拟UINavigationController的新的在iOS8中使用Swift点击时如何隐藏/显示选项卡栏,ios,swift,ios8,uitabbarcontroller,Ios,Swift,Ios8,Uitabbarcontroller,我试图用一个选项卡栏模拟UINavigationController的新的隐藏栏。我已经看到了很多关于这一点的答案,或者指向在viewController上按下时设置hidesbottomBar,这只会完全隐藏它,而不会在点击时隐藏它 @IBAction func tapped(sender: AnyObject) { // what goes here to show/hide the tabBar ??? } 提前谢谢 编辑:根据下面的建议,我试过了 self.tabBar
隐藏栏。我已经看到了很多关于这一点的答案,或者指向在viewController上按下时设置hidesbottomBar,这只会完全隐藏它,而不会在点击时隐藏它
@IBAction func tapped(sender: AnyObject) {
// what goes here to show/hide the tabBar ???
}
提前谢谢
编辑:根据下面的建议,我试过了
self.tabBarController?.tabBar.hidden = true
这确实隐藏了选项卡栏(点击时切换真/假),但没有动画。不过,我会将此作为一个单独的问题提问。我在ObjC中使用tabBar.hidden=YES在某些情况下隐藏选项卡栏。不过,我还没有尝试将其连接到tap事件。经过多次搜索和尝试各种方法,使用Swift优雅地隐藏/显示UITabBar,我能够将其转换为Swift:
func setTabBarVisible(visible: Bool, animated: Bool) {
//* This cannot be called before viewDidLayoutSubviews(), because the frame is not set before this time
// bail if the current state matches the desired state
if (tabBarIsVisible() == visible) { return }
// get a frame calculation ready
let frame = self.tabBarController?.tabBar.frame
let height = frame?.size.height
let offsetY = (visible ? -height! : height)
// zero duration means no animation
let duration: TimeInterval = (animated ? 0.3 : 0.0)
// animate the tabBar
if frame != nil {
UIView.animate(withDuration: duration) {
self.tabBarController?.tabBar.frame = frame!.offsetBy(dx: 0, dy: offsetY!)
return
}
}
}
func tabBarIsVisible() -> Bool {
return (self.tabBarController?.tabBar.frame.origin.y)! < self.view.frame.maxY
}
// Call the function from tap gesture recognizer added to your view (or button)
@IBAction func tapped(_ sender: Any?) {
setTabBarVisible(visible: !tabBarIsVisible(), animated: true)
}
func settabbar可见(可见:Bool,动画:Bool){
//*无法在viewDidLayoutSubviews()之前调用此函数,因为在此时间之前未设置帧
//如果当前状态与所需状态相匹配,则进行保释
如果(tabBarIsVisible()==可见){return}
//准备好框架计算
设frame=self.tabBar控制器?.tabBar.frame
让高度=框架?.size.height
let offsetY=(可见?-height!:height)
//零持续时间意味着没有动画
让持续时间:时间间隔=(动画?0.3:0.0)
//设置选项卡栏的动画
如果帧!=nil{
UIView.animate(withDuration:duration){
self.tabBar控制器?.tabBar.frame=frame!.offsetBy(dx:0,dy:offsetY!)
返回
}
}
}
func tabBarIsVisible()->Bool{
返回(self.tabBar控制器?.tabBar.frame.origin.y)!
我不得不对这个问题的公认答案稍加修改。它隐藏了条形图,但我的视图没有适当调整大小,因此在底部留下了一个空间
下面的代码成功地设置了隐藏选项卡栏的动画,同时调整了视图的大小以避免该问题
为Swift 3更新(现在代码不那么难看)
func settabbar可见(可见:Bool,动画:Bool){
guard let frame=self.tabBar控制器?.tabBar.frame-else{return}
let height=frame.size.height
let offsetY=(可见?-高度:高度)
让持续时间:时间间隔=(动画?0.3:0.0)
UIView.animate(withDuration:duration,
延迟:0.0,
选项:UIViewAnimationOptions.curveEaseIn,
动画:{[弱自我]()->在中无效
guard let weakSelf=self-else{return}
weakSelf.tabBar控制器?.tabBar.frame=frame.offsetBy(dx:0,dy:offsetY)
weakSelf.view.frame=CGRect(x:0,y:0,宽度:weakSelf.view.frame.width,高度:weakSelf.view.frame.height+offsetY)
weakSelf.view.setNeedsDisplay()的
weakSelf.view.layoutifneed()文件
})
}
func handleTap(识别器:UITapGestureRecognitizer){
setTabBarVisible(可见:!tabBarIsVisible(),动画:true)
}
func tabBarIsVisible()->Bool{
guard let tabBar=tabBarController?.tabBar else{return false}
返回tabBar.frame.origin.y
较旧的Swift 2版本
func setTabBarVisible(visible: Bool, animated: Bool) {
// hide tab bar
let frame = self.tabBarController?.tabBar.frame
let height = frame?.size.height
var offsetY = (visible ? -height! : height)
println ("offsetY = \(offsetY)")
// zero duration means no animation
let duration:NSTimeInterval = (animated ? 0.3 : 0.0)
// animate tabBar
if frame != nil {
UIView.animateWithDuration(duration) {
self.tabBarController?.tabBar.frame = CGRectOffset(frame!, 0, offsetY!)
self.view.frame = CGRectMake(0, 0, self.view.frame.width, self.view.frame.height + offsetY!)
self.view.setNeedsDisplay()
self.view.layoutIfNeeded()
return
}
}
}
@IBAction func handleTap(recognizer: UITapGestureRecognizer) {
setTabBarVisible(!tabBarIsVisible(), animated: true)
}
func tabBarIsVisible() -> Bool {
return self.tabBarController?.tabBar.frame.origin.y < UIScreen.mainScreen().bounds.height
}
func settabbar可见(可见:Bool,动画:Bool){
//隐藏选项卡栏
设frame=self.tabBar控制器?.tabBar.frame
让高度=框架?.size.height
变量offsetY=(可见?-height!:height)
println(“offsetY=\(offsetY)”)
//零持续时间意味着没有动画
let duration:NSTimeInterval=(动画?0.3:0.0)
//设置选项卡栏的动画
如果帧!=nil{
UIView.animateWithDuration(持续时间){
self.tabBar控制器?.tabBar.frame=CGRectOffset(frame!、0、offsetY!)
self.view.frame=CGRectMake(0,0,self.view.frame.width,self.view.frame.height+offsetY!)
self.view.setNeedsDisplay()
self.view.layoutifneed()
返回
}
}
}
@iAction func handleTap(识别器:UITapGestureRecognitizer){
setTabBarVisible(!tabBarIsVisible(),已设置动画:true)
}
func tabBarIsVisible()->Bool{
返回self.tabBar控制器?.tabBar.frame.origin.y
您只需将此行添加到swift中的ViewDidLoad():
self.tabBarController?.tabBar.hidden = true
我喜欢迈克尔·坎普尔的回答。如果有人感兴趣,这里的代码与extension相同:
Swift 2.3
extension UITabBarController {
func setTabBarVisible(visible:Bool, animated:Bool) {
// bail if the current state matches the desired state
if (tabBarIsVisible() == visible) { return }
// get a frame calculation ready
let frame = self.tabBar.frame
let height = frame.size.height
let offsetY = (visible ? -height : height)
// animate the tabBar
UIView.animateWithDuration(animated ? 0.3 : 0.0) {
self.tabBar.frame = CGRectOffset(frame, 0, offsetY)
self.view.frame = CGRectMake(0, 0, self.view.frame.width, self.view.frame.height + offsetY)
self.view.setNeedsDisplay()
self.view.layoutIfNeeded()
}
}
func tabBarIsVisible() ->Bool {
return self.tabBar.frame.origin.y < CGRectGetMaxY(self.view.frame)
}
}
extension UIViewController {
func setTabBarVisible(visible: Bool, animated: Bool) {
//* This cannot be called before viewDidLayoutSubviews(), because the frame is not set before this time
// bail if the current state matches the desired state
if (isTabBarVisible == visible) { return }
// get a frame calculation ready
let frame = self.tabBarController?.tabBar.frame
let height = frame?.size.height
let offsetY = (visible ? -height! : height)
// zero duration means no animation
let duration: TimeInterval = (animated ? 0.3 : 0.0)
// animate the tabBar
if frame != nil {
UIView.animate(withDuration: duration) {
self.tabBarController?.tabBar.frame = frame!.offsetBy(dx: 0, dy: offsetY!)
return
}
}
}
var isTabBarVisible: Bool {
return (self.tabBarController?.tabBar.frame.origin.y ?? 0) < self.view.frame.maxY
}
}
扩展控制器{
func settabbar可见(可见:Bool,动画:Bool){
//如果当前状态与所需状态相匹配,则进行保释
如果(tabBarIsVisible()==可见){return}
//准备好框架计算
设frame=self.tabBar.frame
let height=frame.size.height
let offsetY=(可见?-高度:高度)
//设置选项卡栏的动画
UIView.animateWithDuration(动画?0.3:0.0){
self.tabBar.frame=CGRectOffset(frame,0,offsetY)
self.view.frame=CGRectMake(0,0,self.view.frame.width,self.view.frame.height+offsetY)
self.view.setNeedsDisplay()
self.view.layoutifneed()
}
}
func tabBarIsVisible()->Bool{
返回self.tabBar.frame.origin.y
Swift 3
extension UITabBarController {
func setTabBarVisible(visible:Bool, animated:Bool) {
// bail if the current state matches the desired state
if (tabBarIsVisible() == visible) { return }
// get a frame calculation ready
let frame = self.tabBar.frame
let height = frame.size.height
let offsetY = (visible ? -height : height)
// animate the tabBar
UIView.animateWithDuration(animated ? 0.3 : 0.0) {
self.tabBar.frame = CGRectOffset(frame, 0, offsetY)
self.view.frame = CGRectMake(0, 0, self.view.frame.width, self.view.frame.height + offsetY)
self.view.setNeedsDisplay()
self.view.layoutIfNeeded()
}
}
func tabBarIsVisible() ->Bool {
return self.tabBar.frame.origin.y < CGRectGetMaxY(self.view.frame)
}
}
extension UIViewController {
func setTabBarVisible(visible: Bool, animated: Bool) {
//* This cannot be called before viewDidLayoutSubviews(), because the frame is not set before this time
// bail if the current state matches the desired state
if (isTabBarVisible == visible) { return }
// get a frame calculation ready
let frame = self.tabBarController?.tabBar.frame
let height = frame?.size.height
let offsetY = (visible ? -height! : height)
// zero duration means no animation
let duration: TimeInterval = (animated ? 0.3 : 0.0)
// animate the tabBar
if frame != nil {
UIView.animate(withDuration: duration) {
self.tabBarController?.tabBar.frame = frame!.offsetBy(dx: 0, dy: offsetY!)
return
}
}
}
var isTabBarVisible: Bool {
return (self.tabBarController?.tabBar.frame.origin.y ?? 0) < self.view.frame.maxY
}
}
扩展UIViewController{
func settabbar可见(可见:Bool,动画:Bool){
//*无法在viewDidLayoutSubviews()之前调用此函数,因为在此时间之前未设置帧
//如果当前状态与所需状态相匹配,则进行保释
如果(isTabBarVisible==可见){return}
//准备好框架计算
设frame=self.tabbar控件
UIView.animateWithDuration(0.2, animations: {
self.tabBarController?.tabBar.hidden = true
})
func setTabBarVisible(visible:Bool, animated:Bool) {
//* This cannot be called before viewDidLayoutSubviews(), because the frame is not set before this time
// bail if the current state matches the desired state
if (tabBarIsVisible() == visible) { return }
// get a frame calculation ready
let frame = self.tabBarController?.tabBar.frame
let height = frame?.size.height
let offsetY = (visible ? -height! : height)
// zero duration means no animation
let duration:TimeInterval = (animated ? 0.3 : 0.0)
// animate the tabBar
if frame != nil {
UIView.animate(withDuration: duration) {
self.tabBarController?.tabBar.frame = (self.tabBarController?.tabBar.frame.offsetBy(dx: 0, dy: offsetY!))!
return
}
}
}
func tabBarIsVisible() ->Bool {
return (self.tabBarController?.tabBar.frame.origin.y)! < self.view.frame.midY
}
override func viewWillAppear(_ animated: Bool) {
self.tabBarController?.tabBar.isHidden = true
}
override func viewDidDisappear(_ animated: Bool) {
self.tabBarController?.tabBar.isHidden = false
}