UITabBarController 上的快速 float 按钮

标签 swift uitabbarcontroller uitabbar floating-action-button

我有一个包含 3 个部分的标签栏 Controller 。我还为特殊功能创建了一个 float 按钮,但现在它只在一个部分上。我想添加一次这个按钮并在所有部分显示它。所以你可以通过所有应用程序使用这个按钮。

这可能吗?

可以将这个按钮添加到 UITabBarController。是否可以?你有什么想法吗?

谢谢

最佳答案

swift 4

你需要子类 TabBarController 来操作它。

import UIKit

class CustomTabBarViewController: UITabBarController {
let btn = UIButton() //Here create your button

override func viewDidLoad() {
    super.viewDidLoad()

    //Here set up your button

    self.view.insertSubview(btn, belowSubview: self.tabBar) //<--This is important. 
   //With this call you have a button that is over all view but under the tab bar. 
   //So if you have a round button that it expanded on click, the expanded 
   //part could be hidden by the tab bar, if it is near to tab bar.


   //Other button's setting if you need

    btn.translatesAutoresizingMaskIntoConstraints = false //<- Important
   //Because you are adding the button programmatically you need set the
   //constraints. to do it, you need set to false the autoresizing mask.

    //Here set the constraints.
    //These constraints put the button in the lower right corner, over the tab bar
    let viewDictionary = ["button":btn, "tabBar":self.tabBar]
    let btn_V = NSLayoutConstraint.constraints(withVisualFormat: "V:[button(340)]", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: viewDictionary)
    let btn_H = NSLayoutConstraint.constraints(withVisualFormat: "H:[button(340)]", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: viewDictionary)
    let btn_POS_V = NSLayoutConstraint.constraints(withVisualFormat: "V:[button]-(-130)-[tabBar]", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: viewDictionary)
    let btn_POS_H = NSLayoutConstraint.constraints(withVisualFormat: "H:[button]-(-129)-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: viewDictionary)

    btn.addConstraints(btn_V)
    btn.addConstraints(btn_H)
    self.view.addConstraints(btn_POS_H)
    self.view.addConstraints(btn_POS_V)


}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


/*
// MARK: - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    // Get the new view controller using segue.destinationViewController.
    // Pass the selected object to the new view controller.
}
*/ 

}

这是结果 First Image

Second Image

Button closed

关于UITabBarController 上的快速 float 按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50777578/

相关文章:

ios - 打开和关闭 UINavigationController

ios - iOS TabBarViewController 中的全局变量

ios - UIPageControl 隐藏在 UITabBar 下

generics - 如何在 Swift 中使用泛型子类化另一个类?

swift - 如何在 .playground 中写入文件?

ios - Swift,来自 AnyObject 的 Self

ios - 关闭 UITabBarController 并推送登录 View Controller

iphone - 增加标签栏项目之一的高度

ios - 从自定义 UITableViewCell 委托(delegate) TabBar

ios - 如何在堆栈 View 中设置纵横比?