简体   繁体   English

切换屏幕时,iOS Swift浮动操作按钮会移动

[英]iOS Swift Floating Action Button Moves When Changing Screens

I am having some trouble with my Floating Action Button in Swift. 我在Swift中的“浮动操作按钮”遇到了一些麻烦。 I am using Floaty found here: https://github.com/kciter/Floaty . 我正在使用在这里找到的Floaty: https://github.com/kciter/Floaty : https://github.com/kciter/Floaty So it works well on the first time I click the "Calls" screen, but when I switch to another tab, it goes behind the tab bar. 因此,它在我第一次单击“呼叫”屏幕时效果很好,但是当我切换到另一个选项卡时,它位于选项卡栏的后面。 The code I use to set up the FAB can be found below. 我用来设置FAB的代码可以在下面找到。

What I think is going on is that whenever the tabs are switched, the view alternates between recognizing the tabBar and Safe Inset Areas, but sometimes it doesn't and only the paddingX and paddingY values I supplied in the beginning are supported. 我认为发生的事情是,每当切换选项卡时,视图就会在识别tabBar和安全插入区域之间交替显示,但有时却不会,并且仅支持我一开始提供的paddingX和paddingY值。

I have tried switching between a TableViewController and a ViewController with a tableView in it, but that doesn't really seem to be the problem. 我曾尝试在TableViewController和带有TableView的ViewController之间进行切换,但这似乎并不是问题所在。 I have also tried resetting the padding on every viewWillAppear() just to make sure it is at the right height, but that did not work either. 我还尝试过重置每个viewWillAppear()上的填充,以确保其正确的高度,但这也不起作用。 Lastly, I tried to add constraints to the FAB. 最后,我尝试为FAB添加约束。 That was a complex process, that I don't think I did correctly, so it didn't end up working either. 那是一个复杂的过程,我认为我做得不正确,因此也没有奏效。 The code I used can be seen below. 我使用的代码可以在下面看到。

Does anyone have any suggestions as to why this might be happening, or what I can do to make it work? 是否有人对为什么会发生这种情况提出任何建议,或者我能做些什么使它起作用? Right now, my other option is to either get rid of the FAB entirely, or switch to some other library like Google's Material Design Components and use that 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.
    }

}

While I'm not familiar with Floaty, my guess would be that it's your tableview being underneath the tab bar that's causing it to fluctuate in it's location. 虽然我对Floaty并不熟悉,但我猜想是它的表视图位于选项卡栏的下面,这导致它的位置发生波动。

For example if I added a generic UIView like this: 例如,如果我添加了这样的通用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)

It would be underneath the tab bar. 它会在选项卡栏的下面。

If you're looking for a quick fix, and don't necessarily need your tableview to extend behind your tab bar, you should just be able to add this line: 如果您正在寻找一种快速修复方法,并且不一定需要将表格视图扩展到选项卡栏的后面,则只需添加以下行即可:

self.edgesForExtendedLayout.remove(UIRectEdge.bottom)

before setting up the Floaty in viewDidLoad and your tableview won't extend below the bottom tab bar. 在viewDidLoad中设置Floaty之前,您的tableview不会在底部标签栏下方扩展。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM