swift - UITabBarController 上的快速浮动按钮
问题描述
我有一个带有 3 部分的标签栏控制器。我还为特殊功能创建了一个浮动按钮,但现在它只在一个部分上。我想一次添加此按钮并在所有部分中显示它。因此,您可以通过所有 App 使用此按钮。
可能吗?
一个想法可以是将此按钮添加到 UITabBarController。可能吗?你有什么想法吗?
谢谢
解决方案
斯威夫特 4
您需要子类 TabBarController e 操作它。
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.
}
*/
}
推荐阅读
- ruby-on-rails - 取消转义 HTML 实体
- python - 用视频覆盖视频 - Python
- ios - 使用 InAppPurchase 购买多个订阅计划
- node.js - jwt.verify 函数不返回任何内容,api 无法得到任何响应
- c# - 实体框架不会从 MVC 中的实体数据模型(来自现有数据库)创建上下文和表类(.cs)
- mysql - 如何在 MySQL 和 MySQL Workbench 中实现 PASSWORD() 方法
- angular - 向 ViewChildren 添加动态组件
- reactjs - 如何根据放置选项选择显示两个链接?
- c++ - 是否可以随意在发布版本上生成完整的内存转储?
- html - 如何根据css中的当前页面更改边框颜色