如何解决从 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)
}
}
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()
}
}
解决方法
发现问题,是自动布局的问题,我把自动布局改成这样:
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)
// ])
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。