程序问答   发布时间:2022-06-01  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了从 UIButton swift 呈现 UICollectionView大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决从 UIButton swift 呈现 UICollectionView?

开发过程中遇到从 UIButton swift 呈现 UICollectionView的问题如何解决?下面主要结合日常开发的经验,给出你关于从 UIButton swift 呈现 UICollectionView的解决方法建议,希望对你解决从 UIButton swift 呈现 UICollectionView有所启发或帮助;

首先,我希望我不会违反任何规则或延长时间,但我真的被困住并花了一整天的时间,所以我很乐意获得帮助:)

好的,所以我有一个使用 Panmodel 3rd party 的弹出视图控制器,我在 UIbutton 自定义单元格中有一个 UItableVIEw,现在我想在用户按下按钮,现在我按照 Ray 教程(Link)了解如何制作自定义日历,但它仍然对我不起作用,出于某种原因,当我按下按钮时,它只显示清晰的视图 + 冻结我的屏幕:|,我将发布我的 tableVIEw 设置的代码 + UIbutton 的单元格,不打算发布日历的代码,因为它很长而且我不想发送垃圾邮件,如果需要我会发布:) 再次感谢您的帮助,真的花了一整天的时间,发现自己没有解决方案,所以这是我的代码:

代码:tableVIEwVC:

    import UIKit
    import PanModal
       
    class FiltertableVIEwController: UItableVIEwController,PanModalPresentable {
        
        var panScrollable: UIScrollVIEw? {
            return tableVIEw
        }
        
        var albumsPickerIndexPath: IndexPath? //  indexPath of the currently shown albums picker in tablevIEw.
        
        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,wIDth: 380,height: 35))
        
            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" the 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()
            
            if indexPath.row == 4 {
                let pickerController = CalendarPickerVIEwController(
                  baseDate: Date(),SELEctedDateChanged: { [weak self] date in
                  guard let self = self else { return }
    
        //          self.item.date = date
                  self.tableVIEw.reloadRows(at: [IndexPath(row: 3,section: 0)],with: .fadE)
                  })
    
                present(pickerController,animated: true,completion: nil)
            }
        }
        
        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
              self.tableVIEw.reloadRows(at: [IndexPath(row: 3,with: .fadE)
              })
    
            present(pickerController,completion: nil)
        }
    
        func dIDbuttonTopressed() {
            print("button To is pressed")
            //Todo: Present our custom calendar
            let vcTodisplay = CalendarPickerVIEwController(baseDate: Date()) { (datE) in
                
                self.tableVIEw.reloadRows(at: [IndexPath(row: 3,with: .fadE)
            }
    
            self.present(vcTodisplay,completion: nil)
        }
    }
@H_502_13@

自定义单元格代码:

    import UIKit
    
    // MARK: - Class Protocols:
    
    protocol DatestableVIEwCellDelegate { // a delegate to tell when the user SELEcted the button:
        func dIDbuttonFrompressed()
        func dIDbuttonTopressed()
    
    }
    
    class DatesCell: UItableVIEwCell {
    
        @IBOutlet var fromDate: UIbutton!
        
        @IBOutlet var toDate: UIbutton!
        
        var datestableVIEwCellDelegate: DatestableVIEwCellDelegate?
    
        overrIDe func awakeFromNib() {
            super.awakeFromNib()
            // Initialization code
            fromDate.layer.cornerRadius = 5
            toDate.layer.cornerRadius = 5
            
            fromDate.layer.borderWIDth = 1
            toDate.layer.borderWIDth = 1
    
            fromDate.layer.bordercolor = #colorliteral(red: 0.2005972862,green: 0.6100016236,blue: 0.9602670074,Alpha: 1)
            toDate.layer.bordercolor = #colorliteral(red: 0.2005972862,Alpha: 1)
    
            fromDate.layer.masksToBounds = true
            toDate.layer.masksToBounds = true
            
            self.preservesSupervIEwLayoutmargins = false
            self.separatorInset = UIEdgeInsets(top: 0,left: 15,bottom: 0,right: 15)
            self.layoutmargins = UIEdgeInsets(top: 0,right: 15)
        }
    
        // MARK: - UIbutton Methods:
        
        @IBACtion func fromDatebuttonIspressed(_ sender: UIbutton) {
            datestableVIEwCellDelegate?.dIDbuttonFrompressed()
        }
        
        @IBACtion func toDatebuttonIspressed(_ sender: UIbutton) {
            datestableVIEwCellDelegate?.dIDbuttonTopressed()
        }     
    }
@H_502_13@ 

解决方法

发现问题,是自动布局的问题,我把自动布局改成这样:

  override func viewDidLoad() {
    super.viewDidLoad()

    collectionView.BACkgroundColor = .yellow

    
    view.addSubview(dimmedBACkgroundView)
    view.addSubview(collectionView)
    
    var consTraints = [
      dimmedBACkgroundView.leadingAnchor.consTraint(equalTo: view.leadingAnchor),dimmedBACkgroundView.TrailingAnchor.consTraint(equalTo: view.TrailingAnchor),dimmedBACkgroundView.topAnchor.consTraint(equalTo: view.topAnchor),dimmedBACkgroundView.bottomAnchor.consTraint(equalTo: view.bottomAnchor)
    ]
    
    consTraints.append(contentsOf: [
      //1
      collectionView.leadingAnchor.consTraint(
        equalTo: view.readableContentGuide.leadingAnchor),collectionView.TrailingAnchor.consTraint(
        equalTo: view.readableContentGuide.TrailingAnchor),//2
      collectionView.centerYAnchor.consTraint(
        equalTo: view.centerYAnchor,constant: 10),//3
      collectionView.heightAnchor.consTraint(
        equalTo: view.heightAnchor,multiplier: 0.5)
    ])

    NSLayoutConsTraint.activate(consTraints)

//    NSLayoutConsTraint.activate([
//      dimmedBACkgroundView.leadingAnchor.consTraint(equalTo: view.leadingAnchor),//      dimmedBACkgroundView.TrailingAnchor.consTraint(equalTo: view.TrailingAnchor),//      dimmedBACkgroundView.topAnchor.consTraint(equalTo: view.topAnchor),//      dimmedBACkgroundView.bottomAnchor.consTraint(equalTo: view.bottomAnchor)
//    ])
  }

大佬总结

以上是大佬教程为你收集整理的从 UIButton swift 呈现 UICollectionView全部内容,希望文章能够帮你解决从 UIButton swift 呈现 UICollectionView所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。