Ios 将浮动操作按钮添加到选项卡栏控制器内的视图控制器
我使用的是一个自定义浮动操作按钮(),我希望通过编程方式将其添加到UITableViewController中,该控制器位于UITabBarController中。问题是底部选项卡栏覆盖在浮动操作按钮的顶部 在我的表视图控制器内:Ios 将浮动操作按钮添加到选项卡栏控制器内的视图控制器,ios,swift,uitabbarcontroller,floating-action-button,Ios,Swift,Uitabbarcontroller,Floating Action Button,我使用的是一个自定义浮动操作按钮(),我希望通过编程方式将其添加到UITableViewController中,该控制器位于UITabBarController中。问题是底部选项卡栏覆盖在浮动操作按钮的顶部 在我的表视图控制器内: let floaty = Floaty() ... self.view.addSubview(floaty) // also tried: self.tabBarController?.view.addSubview(floaty) 我试着这样设置边距: float
let floaty = Floaty()
...
self.view.addSubview(floaty)
// also tried: self.tabBarController?.view.addSubview(floaty)
我试着这样设置边距:
floaty.layoutMargins = UIEdgeInsets(top: 0, left: 0, bottom: 60, right: 0)
floaty.layoutIfNeeded()
self.view.layoutIfNeeded()
但它不起作用
如何将按钮正确放置在“表视图”控制器中?或者至少,我该如何以编程方式设置底部边距
编辑:
也试过
self.navigationController?.view.addSubview(floaty)
起初,它正确地添加了按钮,但当我切换选项卡时,它会返回到被底部栏覆盖的位置。
在这里:
您不应该在tableView中实现它, 您应该将其添加到您的homeTabs中,您已经在其中实现了您的选项卡 您既不能通过添加为IBOutlet来添加它,也不能将其声明为按钮 这是我的代码(我的竞赛选项卡中有一个浮动按钮) //为ur按钮添加自下而上的前导跟踪约束
@IBOutlet weak var leadBottom: NSLayoutConstraint!
///这导致我的按钮浮动
//这是我的按钮底部对superView.bottom的约束
然后在viewDidLoad中:
NotificationCenter.default.addObserver(self, selector: #selector(leadAnime(_:)),name:NSNotification.Name(rawValue: "animate"), object: nil)
然后,在viewDidLoad中的某些位置添加以下内容:
@objc func leadAnime(_ notification: NSNotification) {
if let count = notification.userInfo?["top"] as? Bool{
if count{
leadBottom.constant = 20
UIView.animate(withDuration: 0.3, animations: {
self.view.layoutIfNeeded()
})
}else{
leadBottom.constant = -100
UIView.animate(withDuration: 0.3, animations: {
self.view.layoutIfNeeded()
})
}
}
}
现在在您的表视图控制器中:
var scrollS :CGFloat = 0
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "animate"), object: nil, userInfo: ["top":false])
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "animate"), object: nil, userInfo: ["top":true]) }
override func scrollViewDidScroll(_ scrollView: UIScrollView) {
if scrollS - scrollView.contentOffset.y < 0{
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "animate"), object: nil, userInfo: ["top":true])
}else{
if scrollS - scrollView.contentOffset.y > 0{
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "animate"), object: nil, userInfo: ["top":false])
}
}
scrollS = scrollView.contentOffset.y
}
override func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
if scrollS - scrollView.contentOffset.y < 0{
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "animate"), object: nil, userInfo: ["top":true])
}else{
if scrollS - scrollView.contentOffset.y > 0{
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "animate"), object: nil, userInfo: ["top":false])
}
}
scrollS = scrollView.contentOffset.y
}
var滚动:CGFloat=0
覆盖函数视图将消失(u动画:Bool){
超级。视图将消失(动画)
NotificationCenter.default.post(名称:NSNotification.name(rawValue:“animate”),对象:nil,用户信息:[“top”:false])
}
覆盖函数视图将出现(uo动画:Bool){
超级。视图将显示(动画)
NotificationCenter.default.post(名称:NSNotification.name(rawValue:“animate”),对象:nil,用户信息:[“top”:true])
重写func scrollViewDidScroll(scrollView:UIScrollView){
如果滚动-scrollView.contentOffset.y<0{
NotificationCenter.default.post(名称:NSNotification.name(rawValue:“animate”),对象:nil,用户信息:[“top”:true])
}否则{
如果滚动-scrollView.contentOffset.y>0{
NotificationCenter.default.post(名称:NSNotification.name(rawValue:“animate”),对象:nil,用户信息:[“top”:false])
}
}
scrollS=scrollView.contentOffset.y
}
重写func scrollView将开始刷新(scrollView:UIScrollView){
如果滚动-scrollView.contentOffset.y<0{
NotificationCenter.default.post(名称:NSNotification.name(rawValue:“animate”),对象:nil,用户信息:[“top”:true])
}否则{
如果滚动-scrollView.contentOffset.y>0{
NotificationCenter.default.post(名称:NSNotification.name(rawValue:“animate”),对象:nil,用户信息:[“top”:false])
}
}
scrollS=scrollView.contentOffset.y
}
您不应该在tableView中实现它,
您应该将其添加到您的homeTabs中,您已经在其中实现了您的选项卡
您既不能通过添加为IBOutlet来添加它,也不能将其声明为按钮
这是我的代码(我的竞赛选项卡中有一个浮动按钮)
//为ur按钮添加自下而上的前导跟踪约束
@IBOutlet weak var leadBottom: NSLayoutConstraint!
///这导致我的按钮浮动
//这是我的按钮底部对superView.bottom的约束
然后在viewDidLoad中:
NotificationCenter.default.addObserver(self, selector: #selector(leadAnime(_:)),name:NSNotification.Name(rawValue: "animate"), object: nil)
然后,在viewDidLoad中的某些位置添加以下内容:
@objc func leadAnime(_ notification: NSNotification) {
if let count = notification.userInfo?["top"] as? Bool{
if count{
leadBottom.constant = 20
UIView.animate(withDuration: 0.3, animations: {
self.view.layoutIfNeeded()
})
}else{
leadBottom.constant = -100
UIView.animate(withDuration: 0.3, animations: {
self.view.layoutIfNeeded()
})
}
}
}
现在在您的表视图控制器中:
var scrollS :CGFloat = 0
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "animate"), object: nil, userInfo: ["top":false])
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "animate"), object: nil, userInfo: ["top":true]) }
override func scrollViewDidScroll(_ scrollView: UIScrollView) {
if scrollS - scrollView.contentOffset.y < 0{
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "animate"), object: nil, userInfo: ["top":true])
}else{
if scrollS - scrollView.contentOffset.y > 0{
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "animate"), object: nil, userInfo: ["top":false])
}
}
scrollS = scrollView.contentOffset.y
}
override func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
if scrollS - scrollView.contentOffset.y < 0{
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "animate"), object: nil, userInfo: ["top":true])
}else{
if scrollS - scrollView.contentOffset.y > 0{
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "animate"), object: nil, userInfo: ["top":false])
}
}
scrollS = scrollView.contentOffset.y
}
var滚动:CGFloat=0
覆盖函数视图将消失(u动画:Bool){
超级。视图将消失(动画)
NotificationCenter.default.post(名称:NSNotification.name(rawValue:“animate”),对象:nil,用户信息:[“top”:false])
}
覆盖函数视图将出现(uo动画:Bool){
超级。视图将显示(动画)
NotificationCenter.default.post(名称:NSNotification.name(rawValue:“animate”),对象:nil,用户信息:[“top”:true])
重写func scrollViewDidScroll(scrollView:UIScrollView){
如果滚动-scrollView.contentOffset.y<0{
NotificationCenter.default.post(名称:NSNotification.name(rawValue:“animate”),对象:nil,用户信息:[“top”:true])
}否则{
如果滚动-scrollView.contentOffset.y>0{
NotificationCenter.default.post(名称:NSNotification.name(rawValue:“animate”),对象:nil,用户信息:[“top”:false])
}
}
scrollS=scrollView.contentOffset.y
}
重写func scrollView将开始刷新(scrollView:UIScrollView){
如果滚动-scrollView.contentOffset.y<0{
NotificationCenter.default.post(名称:NSNotification.name(rawValue:“animate”),对象:nil,用户信息:[“top”:true])
}否则{
如果滚动-scrollView.contentOffset.y>0{
NotificationCenter.default.post(名称:NSNotification.name(rawValue:“animate”),对象:nil,用户信息:[“top”:false])
}
}
scrollS=scrollView.contentOffset.y
}
您在视图控制器上添加了按钮,该按钮位于选项卡栏控制器下方:
self.view.addSubview(floaty)
当然,选项卡栏将始终位于视图控制器的上方,您将该按钮添加为子视图。
查看图2,在中,您添加了视图控制器上的按钮,该按钮位于选项卡栏控制器下方
self.view.addSubview(floaty)
当然,选项卡栏将始终位于视图控制器的上方,您将该按钮添加为子视图。
请看中的图2,这行代码将解决填充问题:
floaty.paddingY += tabBarController!.tabBar.frame.size.height
如果您希望所有选项卡的晶圆厂均为全局晶圆厂,则应按如下方式添加晶圆厂:
tabBarController!.view.addSubview(floaty)
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
floaty.isHidden = false
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
floaty.isHidden = true
}
否则,如果将其添加到navigationController,并且我假设每个选项卡都有一个不同的选项卡,则它将仅存在于该navigationController的ViewController中。如果将其添加到self.view,也会发生同样的情况,因为它将只存在于该视图控制器中
此外,如果要在某些特定视图控制器中隐藏/取消隐藏晶圆厂,则应使用