![](/img/trans.png)
[英]How to put a floating action button in a tableView in swift in iOS?
[英]iOS Swift Floating Action Button Moves When Changing Screens
我在Swift中的“浮动操作按钮”遇到了一些麻烦。 我正在使用在这里找到的Floaty: https://github.com/kciter/Floaty
: https://github.com/kciter/Floaty
。 因此,它在我第一次单击“呼叫”屏幕时效果很好,但是当我切换到另一个选项卡时,它位于选项卡栏的后面。 我用来设置FAB的代码可以在下面找到。
我认为发生的事情是,每当切换选项卡时,视图就会在识别tabBar和安全插入区域之间交替显示,但有时却不会,并且仅支持我一开始提供的paddingX和paddingY值。
我曾尝试在TableViewController和带有TableView的ViewController之间进行切换,但这似乎并不是问题所在。 我还尝试过重置每个viewWillAppear()上的填充,以确保其正确的高度,但这也不起作用。 最后,我尝试为FAB添加约束。 那是一个复杂的过程,我认为我做得不正确,因此也没有奏效。 我使用的代码可以在下面看到。
是否有人对为什么会发生这种情况提出任何建议,或者我能做些什么使它起作用? 现在,我的另一种选择是要么完全放弃FAB,要么切换到其他一些库,例如Google的Material Design Components,然后使用该FAB。
import UIKit
import Floaty
import Material
class CallCreationViewController: UIViewController, UISearchResultsUpdating, UISearchBarDelegate {
@IBOutlet weak var tableView: UITableView!
let floaty : Floaty = Floaty()
var connections: [User] = []
var filteredConnections : [User] = []
var selectedIds: [String] = []
let inset : CGFloat = 34
let blue : Color = Color(red:0.09, green:0.04, blue:0.35, alpha:1.0)
override func viewDidLoad() {
super.viewDidLoad()
floaty.buttonColor = blue
floaty.plusColor = .white
let voiceItem : FloatyItem = FloatyItem()
voiceItem.title = "Start Voice Call"
voiceItem.icon = UIImage(imageLiteralResourceName: "baseline_phone_white_24pt")
voiceItem.iconTintColor = .white
voiceItem.buttonColor = blue
voiceItem.handler = { item in
self.performSegue(withIdentifier: "startVoiceCall", sender: self)
self.floaty.close()
}
let videoItem : FloatyItem = FloatyItem()
videoItem.title = "Start Video Call"
videoItem.icon = UIImage(imageLiteralResourceName: "baseline_video_call_white_24pt")
videoItem.iconTintColor = .white
videoItem.buttonColor = blue
videoItem.handler = { item in
self.performSegue(withIdentifier: "startVideoCall", sender: self)
self.floaty.close()
}
let broadcastItem : FloatyItem = FloatyItem()
broadcastItem.title = "Start Broadcast"
broadcastItem.icon = UIImage(imageLiteralResourceName: "baseline_settings_input_antenna_white_24pt")
broadcastItem.iconTintColor = .white
broadcastItem.buttonColor = blue
broadcastItem.handler = { item in
self.performSegue(withIdentifier: "startBroadcast", sender: self)
self.floaty.close()
}
floaty.addItem(item: voiceItem)
floaty.addItem(item: videoItem)
floaty.addItem(item: broadcastItem)
floaty.respondsToKeyboard = false
//let constraint = NSLayoutConstraint(item: floaty, attribute: NSLayoutConstraint.Attribute.bottomMargin, relatedBy: NSLayoutConstraint.Relation.equal, toItem: self.tabBarController?.tabBar, attribute: NSLayoutConstraint.Attribute.notAnAttribute, multiplier: 1, constant: 100)
floaty.sticky = true
floaty.paddingX = inset
floaty.paddingY = inset
self.view.addSubview(floaty)
//let constraint = NSLayoutConstraint(item: floaty, attribute: .bottomMargin, relatedBy: .equal, toItem: self.navigationController?.navigationBar, attribute: .top, multiplier: 1, constant: 100)
//floaty.addConstraint(constraint)
self.tableView.dataSource = self
// Do any additional setup after loading the view.
}
}
虽然我对Floaty并不熟悉,但我猜想是它的表视图位于选项卡栏的下面,这导致它的位置发生波动。
例如,如果我添加了这样的通用UIView:
let greenView : UIView = UIView(frame: CGRect(x: self.view.frame.size.width - 100, y: self.view.frame.size.height - 100, width: 100, height: 100))
greenView.backgroundColor = UIColor.green
self.view.addSubview(greenView)
它会在选项卡栏的下面。
如果您正在寻找一种快速修复方法,并且不一定需要将表格视图扩展到选项卡栏的后面,则只需添加以下行即可:
self.edgesForExtendedLayout.remove(UIRectEdge.bottom)
在viewDidLoad中设置Floaty之前,您的tableview不会在底部标签栏下方扩展。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.