Swift UITABBARC控制器上的快速浮动按钮

Swift UITABBARC控制器上的快速浮动按钮,swift,uitabbarcontroller,uitabbar,floating-action-button,Swift,Uitabbarcontroller,Uitabbar,Floating Action Button,我有一个带有3个部分的选项卡栏控制器。我还为特殊功能创建了一个浮动按钮,但现在它只在一个部分上。我想添加这个按钮一次,并显示在所有部分。因此,您可以在所有应用程序中使用此按钮 可能吗 一个想法是将此按钮添加到UITabBarController。可能吗?你知道吗 谢谢您需要将其添加到应用程序的窗口中 let del = UIApplication.shared.delegate as! AppDelegate del.window?.addSubview(btn) 您需要将其添加到应用程序的

我有一个带有3个部分的选项卡栏控制器。我还为特殊功能创建了一个浮动按钮,但现在它只在一个部分上。我想添加这个按钮一次,并显示在所有部分。因此,您可以在所有应用程序中使用此按钮

可能吗

一个想法是将此按钮添加到UITabBarController。可能吗?你知道吗


谢谢

您需要将其添加到应用程序的窗口中

let del = UIApplication.shared.delegate as! AppDelegate

del.window?.addSubview(btn)

您需要将其添加到应用程序的窗口中

let del = UIApplication.shared.delegate as! AppDelegate

del.window?.addSubview(btn)

Swift 4

您需要子类tabbare来操作它

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.
}
*/ 

}
导入UIKit
类CustomTabBarViewController:UITabBarController{
让btn=UIButton()//在这里创建按钮
重写func viewDidLoad(){
super.viewDidLoad()
//这里设置您的按钮

self.view.insertSubview(btn,belowSubview:self.tabBar)/Swift 4

您需要子类tabbare来操作它

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.
}
*/ 

}
导入UIKit
类CustomTabBarViewController:UITabBarController{
让btn=UIButton()//在这里创建按钮
重写func viewDidLoad(){
super.viewDidLoad()
//这里设置您的按钮

self.view.insertSubview(btn,belowSubview:self.tabBar)//非常感谢。有办法制作故事板吗?或者只能通过编程实现吗?我尝试了你的代码,但没有成功。你看不到按钮,因为它位于选项卡栏包含的viewController下。有什么建议吗?非常感谢。有办法制作故事板吗?或者只能通过编程实现吗?我尝试了你的代码,但没有成功on不工作。您看不到该按钮,因为它位于选项卡栏包含的viewController下。有什么建议吗?