【问题标题】:How to put a floating action button in a tableView in swift in iOS?如何在 iOS 的 swift 中将浮动操作按钮放在 tableView 中?
【发布时间】:2018-08-22 13:43:19
【问题描述】:

我正在尝试使用 iOS 中的浮动操作按钮强加于表格视图,以便我可以在表格视图中添加项目。请帮我写代码。

【问题讨论】:

  • 简单地说,最简单的解决方案是将按钮放在包含tableview的视图中。这样,您就不必担心 tableview 移动时的翻译。这是 Arindam 的回答所采取的基本方法。

标签: ios swift uitableview floating-action-button


【解决方案1】:

这是它的完整代码。它是在不使用情节提要的情况下完成的。

表格视图:

import UIKit

class ViewController: UIViewController, UITableViewDataSource {

    let nameArray = ["India","Usa","UK"]

    let tableView: UITableView = {
        let table = UITableView()
        table.translatesAutoresizingMaskIntoConstraints = false
        return table
    }()



    let btnFloating : UIButton = {
        let floating = UIButton()
        floating.translatesAutoresizingMaskIntoConstraints = false
        floating .backgroundColor = .cyan
        floating.setTitle("ADD", for: .normal)
        return floating
    }()


    override func viewDidLoad() {
        super.viewDidLoad()
        view.addSubview(tableView)
        tableView.addSubview(btnFloating)
        tableView.dataSource = self
        setuoConstrain()
        //Set the action of add button
        btnFloating.addTarget(self, action: #selector(btnAddTapp(sender:)), for: .touchUpInside)
    }

    func setuoConstrain(){
        tableView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
        tableView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
        tableView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
        tableView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true

        //Constrain For Button :
        btnFloating.heightAnchor.constraint(equalToConstant: 64).isActive = true
        btnFloating.widthAnchor.constraint(equalToConstant: 64).isActive = true
        btnFloating.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -24).isActive = true
        btnFloating.bottomAnchor.constraint(equalTo: self.view.bottomAnchor, constant: -36).isActive = true


    }

    //This function is for add button . What action you want , can put inside this function
    @objc func btnAddTapp(sender: UIButton){
        print("add button tapp")
    }


    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return nameArray.count
    }



    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let  nameCell = NameTableCell(style: .default, reuseIdentifier: "NameTableCell")
        nameCell.lblName.text = nameArray[indexPath.row]
        return nameCell
    }
}

TableViewCell:

import UIKit

class NameTableCell: UITableViewCell {

    let lblName: UILabel = {
        let name = UILabel()
        name.translatesAutoresizingMaskIntoConstraints = false
        return name
    }()



    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.addSubview(lblName)

        constrain()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }


    func constrain(){
        lblName.centerXAnchor.constraint(equalTo: self.centerXAnchor).isActive = true

    }
}

【讨论】:

    【解决方案2】:
    func setupFloatingActionButton() {
    
        Floaty.global.button.buttonImage = UIImage(named: "icon-social")
        Floaty.global.button.buttonColor = UIColor.white
        let facebookItem = FloatyItem()
        facebookItem.icon = UIImage(named: "icon-facebook")
        facebookItem.title = "Facebook"
        Floaty.global.button.addItem(item: facebookItem)
    
        let gmailItem = FloatyItem()
        Floaty.global.button.addItem("Gmail", icon: UIImage(named: "icon-gmail"), handler: {_
            in
            print("Gmail Button tapp")
        })   
    
        let twitterItem = FloatyItem()
        Floaty.global.button.addItem("Twitter", icon: UIImage(named: "icon-twitter"), handler: {_ in
            print("twitter Button tapp")
        })
    
        //Floaty.global.button.animationSpeed = 0.50
        Floaty.global.button.openAnimationType = .fade
        //Floaty.global.button.rotationDegrees = 90.00
        Floaty.global.show()
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-05-25
      • 2021-07-15
      • 1970-01-01
      • 1970-01-01
      • 2021-01-27
      • 2016-01-25
      • 1970-01-01
      相关资源
      最近更新 更多