Ios 将约束设置为UITableView页脚Swft

Ios 将约束设置为UITableView页脚Swft,ios,swift,uitableview,autolayout,Ios,Swift,Uitableview,Autolayout,我试着用UIButton来放置页脚,现在我只用frame来放置,当我在苹果的iPhone上尝试时,由于我没有设置任何自动布局,所以按钮放置不好,问题是我正在尝试将自动布局设置到页脚,但一直失败,我也不确定是否有可能这样做,希望得到一个方便的帮助,甚至提示:)。 以下是我的tableView代码: import UIKit import PanModal protocol FilterTableViewDelegate { func didUpdateSelectedDate(_ dat

我试着用UIButton来放置页脚,现在我只用frame来放置,当我在苹果的iPhone上尝试时,由于我没有设置任何自动布局,所以按钮放置不好,问题是我正在尝试将自动布局设置到页脚,但一直失败,我也不确定是否有可能这样做,希望得到一个方便的帮助,甚至提示:)。 以下是我的tableView代码:

import UIKit
import PanModal

protocol FilterTableViewDelegate {
    func didUpdateSelectedDate(_ date: Date)
}

class FilterTableViewController: UITableViewController, PanModalPresentable {
    
    var panScrollable: UIScrollView? {
        return tableView
    }
    
    
    var albumsPickerIndexPath: IndexPath? //  indexPath of the currently shown albums picker in tableview.
    var delegate: FilterTableViewDelegate?
    
    var datesCell = DatesCell()
    
    
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setupTableView()
        //        registerTableViewCells()
        
    }
    
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        //        tableView.frame = view.bounds
    }
    
    override var preferredStatusBarStyle: UIStatusBarStyle {
        return .lightContent
    }
    
    
    
    // MARK: - View Configurations
    
    func setupTableView() {
        
        
        tableView.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 0).isActive = true
        tableView.topAnchor.constraint(equalTo: view.topAnchor, constant: 0).isActive = true
        tableView.rightAnchor.constraint(equalTo: view.rightAnchor, constant: 0).isActive = true
        tableView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: 0).isActive = true
        
        
        tableView.separatorStyle = .singleLine
        tableView.isScrollEnabled = false
        tableView.allowsSelection = true
        
        
        tableView.rowHeight = UITableView.automaticDimension
        tableView.estimatedRowHeight = 600
        
        
        tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
        tableView.backgroundColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
        
    }
    
    
    
    func indexPathToInsertDatePicker(indexPath: IndexPath) -> IndexPath {
        if let albumsPickerIndexPath = albumsPickerIndexPath, albumsPickerIndexPath.row < indexPath.row {
            return indexPath
        } else {
            return IndexPath(row: indexPath.row + 1, section: indexPath.section)
        }
    }
    
    
    // MARK: - UITableViewDataSource
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // If datePicker is already present, we add one extra cell for that
        if albumsPickerIndexPath != nil {
            return 5 + 1
        } else {
            return 5
        }
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        switch indexPath.row {
        case 0:
            
            let byActivityCell = UINib(nibName: "byActivityCell",bundle: nil)
            self.tableView.register(byActivityCell,forCellReuseIdentifier: "byActivityCell")
            let activityCell = tableView.dequeueReusableCell(withIdentifier: "byActivityCell", for: indexPath) as! byActivityCell
            activityCell.selectionStyle = .none
            
            return activityCell
            
        case 1:
            let byTypeCell = UINib(nibName: "ByType",bundle: nil)
            self.tableView.register(byTypeCell,forCellReuseIdentifier: "byTypeCell")
            let typeCell = tableView.dequeueReusableCell(withIdentifier: "byTypeCell", for: indexPath) as! ByType
            typeCell.selectionStyle = .none
            return typeCell
            
        case 2:
            let byHashtagsCell = UINib(nibName: "ByHashtags",bundle: nil)
            self.tableView.register(byHashtagsCell,forCellReuseIdentifier: "byHashtagsCell")
            let hashtagsCell = tableView.dequeueReusableCell(withIdentifier: "byHashtagsCell", for: indexPath) as! ByHashtags
            hashtagsCell.selectionStyle = .none
            
            return hashtagsCell
            
        case 3:
            let byDatesCell = UINib(nibName: "DatesCell",bundle: nil)
            self.tableView.register(byDatesCell,forCellReuseIdentifier: "byDatesCell")
            let datesCell = tableView.dequeueReusableCell(withIdentifier: "byDatesCell", for: indexPath) as! DatesCell
            datesCell.selectionStyle = .none
            datesCell.datesTableViewCellDelegate = self
            
            return datesCell
            
            
        case 4:
            let byAlbumCell = UINib(nibName: "AlbumCell",bundle: nil)
            self.tableView.register(byAlbumCell,forCellReuseIdentifier: "byAlbumCell")
            let albumCell = tableView.dequeueReusableCell(withIdentifier: "byAlbumCell", for: indexPath) as! AlbumCell
            albumCell.configureCell(choosenAlbum: "Any")
            albumCell.selectionStyle = .none
            
            return albumCell
            
            
            
        case 5:
            let albumPickerCell = UINib(nibName: "AlbumsPickerTableViewCell", bundle: nil)
            self.tableView.register(albumPickerCell, forCellReuseIdentifier: "albumPickerCell")
            let albumsPicker = tableView.dequeueReusableCell(withIdentifier: "albumPickerCell", for: indexPath) as! AlbumsPickerTableViewCell
            
            
            return albumsPicker
            
            
        default:
            return UITableViewCell()
        }
        
    }
    
    
    
    
    
    // MARK: - footer Methods:
    override func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        return getfooterView()
    }
    
    func getfooterView() -> UIView
    {
        let footerView = UIView(frame: CGRect(x: 0, y: 0, width: tableView.frame.width, height: 400))
        let applyFiltersBtn = UIButton(frame: CGRect(x: 0, y: 0, width: 380, height: 40))
        
        
        applyFiltersBtn.center = footerView.center
        
        applyFiltersBtn.layer.cornerRadius = 12
        applyFiltersBtn.layer.masksToBounds = true
        applyFiltersBtn.setTitle("Apply Filters", for: .normal)
        applyFiltersBtn.backgroundColor = #colorLiteral(red: 0.1957295239, green: 0.6059523225, blue: 0.960457623, alpha: 1)
        
        
        //        doneButton.addTarget(self, action: #selector(hello(sender:)), for: .touchUpInside)
        
        
        
        footerView.addSubview(applyFiltersBtn)
        
        
        return footerView
    }
    
    override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 10
    }
    
    
    
    
    
    
    // MARK: TableViewDelegate Methods:
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: false)
        
        
        tableView.beginUpdates()
        
        // 1 - We Delete the UIPicker when the user "Deselect" thr row.
        if let datePickerIndexPath = albumsPickerIndexPath,   datePickerIndexPath.row - 1 == indexPath.row {
            tableView.deleteRows(at: [datePickerIndexPath], with: .fade)
            self.albumsPickerIndexPath = nil
        } else {
            // 2
            //            if let datePickerIndexPath = albumsPickerIndexPath {
            //                tableView.deleteRows(at: [datePickerIndexPath], with: .fade)
            //            }
            albumsPickerIndexPath = indexPathToInsertDatePicker(indexPath: indexPath)
            tableView.insertRows(at: [albumsPickerIndexPath!], with: .fade)
            tableView.deselectRow(at: indexPath, animated: true)
        }
        
        
        
        tableView.endUpdates()
        
    }
    
    
    override func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath? {
        if indexPath.row == 4  {
            return indexPath
        } else {
            return nil
        }
        
        
    }
    
    
}


extension FilterTableViewController: DatesTableViewCellDelegate {
    
    func didButtonFromPressed() {
        print("Button From is Pressed")
        let pickerController = CalendarPickerViewController(
            baseDate: Date(),
            selectedDateChanged: { [weak self] date in
                guard let self = self else { return }
                
                //          self.item.date = date
                //TODO: Pass the date to the DatesCell to update the UIButtons.
                self.delegate?.didUpdateSelectedDate(date)
                
                self.tableView.reloadRows(at: [IndexPath(row: 3, section: 0)], with: .fade)
            })
        
        present(pickerController, animated: true, completion: nil)
        
    }
    
    
    
    func didButtonToPressed() {
        //TODO: Present our custom calendar
        let calendarController = CalendarPickerViewController(
            baseDate: Date(),
            selectedDateChanged: { [weak self] date in
                guard let self = self else { return }
                
                self.tableView.reloadRows(at: [IndexPath(row: 3, section: 0)], with: .fade)
            })
        
        
        
        self.present(calendarController, animated: true, completion: nil)
        
    }
    
    
}
导入UIKit
导入泛模态
协议筛选器TableViewDelegate{
func didUpdateSelectedDate(u日期:日期)
}
类FilterTableViewController:UITableViewController,PanModalPresentable{
var panScrollable:UIScrollView{
返回表视图
}
var albumsPickerIndexPath:IndexPath?//tableview中当前显示的相册选取器的IndexPath。
变量委托:FilterTableViewDelegate?
var datesCell=datesCell()
重写func viewDidLoad(){
super.viewDidLoad()
setupTableView()
//registerTableViewCells()
}
重写func viewdilayoutsubviews(){
super.viewDidLayoutSubviews()
//tableView.frame=view.bounds
}
重写变量preferredStatusBarStyle:UIStatusBarStyle{
return.lightContent
}
//标记:-查看配置
func setupTableView(){
tableView.leftAnchor.constraint(equalTo:view.leftAnchor,常量:0)。isActive=true
tableView.topAnchor.constraint(equalTo:view.topAnchor,常量:0)。isActive=true
tableView.rightAnchor.constraint(equalTo:view.rightAnchor,常量:0)。isActive=true
tableView.bottomAnchor.constraint(equalTo:view.bottomAnchor,常量:0)。isActive=true
tableView.separatorStyle=.singleLine
tableView.IsScrolEnabled=false
tableView.AllowSelection=true
tableView.rowHeight=UITableView.automaticDimension
tableView.estimatedRowHeight=600
tableView.register(UITableViewCell.self,强制重用标识符:“单元格”)
tableView.backgroundColor=#colorLiteral(红色:1,绿色:1,蓝色:1,alpha:1)
}
func indexPathToInsertDatePicker(indexPath:indexPath)->indexPath{
如果让albumsPickerIndexPath=albumsPickerIndexPath,则albumsPickerIndexPath.rowInt{
//如果datePicker已经存在,我们将为此添加一个额外的单元格
如果albumsPickerIndexPath!=nil{
返回5+1
}否则{
返回5
}
}
重写func tableView(tableView:UITableView,cellForRowAt indexath:indexPath)->UITableViewCell{
切换到xpath.row{
案例0:
让byActivityCell=UINib(nibName:“byActivityCell”,bundle:nil)
self.tableView.register(byActivityCell,强制重用标识符:“byActivityCell”)
让activityCell=tableView.dequeueReusableCell(标识符为:“byActivityCell”,表示:indexPath)作为!byActivityCell
activityCell.selectionStyle=.none
返回活动单元
案例1:
让byTypeCell=UINib(nibName:“ByType”,bundle:nil)
self.tableView.register(byTypeCell,forCellReuseIdentifier:“byTypeCell”)
让typeCell=tableView.dequeueReusableCell(标识符为:“byTypeCell”,表示:indexPath)作为!ByType
typeCell.selectionStyle=.none
返回式单元格
案例2:
let byHashtagsCell=UINib(nibName:“ByHashtags”,bundle:nil)
self.tableView.register(byHashtagsCell,forCellReuseIdentifier:“byHashtagsCell”)
让hashtagsCell=tableView.dequeueReusableCell(标识符为:“byHashtagsCell”,表示:indexPath)作为!ByHashtags
hashtagsCell.selectionStyle=.none
返回hashtagsCell
案例3:
let byDatesCell=UINib(nibName:“DatesCell”,bundle:nil)
self.tableView.register(byDatesCell,forCellReuseIdentifier:“byDatesCell”)
让datesCell=tableView.dequeueReusableCell(标识符为:“byDatesCell”,表示:indexPath)作为!datesCell
datesCell.selectionStyle=.none
datesCell.datesTableViewCellDelegate=self
返回日期查询
案例4:
let byAlbumCell=UINib(nibName:“AlbumCell”,bundle:nil)
self.tableView.register(byAlbumCell,强制重用标识符:“byAlbumCell”)
让albumCell=tableView.dequeueReusableCell(标识符为:“byAlbumCell”,表示:indexPath)作为!albumCell
albumCell.configureCell(选择Album:“任意”)
albumCell.selectionStyle=.none
返回单元
案例5:
让albumPickerCell=UINib(nibName:“AlbumsPickerTableViewCell”,bundle:nil)
self.tableView.register(albumPickerCell,forCellReuseIdentifier:“albumPickerCell”)
让albumsPicker=tableView.dequeueReusableCell(标识符为:“albumPickerCell”,代表:indexPath)作为!AlbumsPickerTableViewCell
回程旋塞器
违约:
返回UITableViewCell()
}
}
//标记:-页脚方法:
重写func tableView(tableView:UITableView,ViewForFooterInstruction节:Int)->UIView{
返回getfooterView()
class MySectionFooterView: UITableViewHeaderFooterView {
    
    let applyFiltersBtn = UIButton()
    
    override init(reuseIdentifier: String?) {
        super.init(reuseIdentifier: reuseIdentifier)
        configureContents()
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
        configureContents()
    }
    
    func configureContents() {

        applyFiltersBtn.translatesAutoresizingMaskIntoConstraints = false
        applyFiltersBtn.layer.cornerRadius = 12
        applyFiltersBtn.layer.masksToBounds = true
        applyFiltersBtn.setTitle("Apply Filters", for: .normal)
        applyFiltersBtn.backgroundColor = #colorLiteral(red: 0.1957295239, green: 0.6059523225, blue: 0.960457623, alpha: 1)

        contentView.addSubview(applyFiltersBtn)

        let g = contentView.layoutMarginsGuide
        
        NSLayoutConstraint.activate([

            // use layoutMarginsGuide for top and bottom
            applyFiltersBtn.topAnchor.constraint(equalTo: g.topAnchor, constant: 0.0),
            applyFiltersBtn.bottomAnchor.constraint(equalTo: g.bottomAnchor, constant: 0.0),
            
            // inset button 20-pts from layoutMarginsGuide on each side
            applyFiltersBtn.leadingAnchor.constraint(equalTo: g.leadingAnchor, constant: 20.0),
            applyFiltersBtn.trailingAnchor.constraint(equalTo: g.trailingAnchor, constant: -20.0),
            
            // make button height 40-pts
            applyFiltersBtn.heightAnchor.constraint(equalToConstant: 40.0),
        ])
        
    }
}

class SectionFooterViewController: UITableViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // register cell class for reuse
        tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
        
        // register Section Footer class for reuse
        tableView.register(MySectionFooterView.self, forHeaderFooterViewReuseIdentifier: "mySectionFooter")

        tableView.sectionFooterHeight = UITableView.automaticDimension
        tableView.estimatedSectionFooterHeight = 50
    }
    
    override func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 8
    }
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
        cell.textLabel?.text = "\(indexPath)"
        return cell
    }

    override func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        let view = tableView.dequeueReusableHeaderFooterView(withIdentifier:"mySectionFooter") as! MySectionFooterView
        return view
    }
}