在我的尝试中,我可以减少左侧的集合视图的宽度,但我无法放大它。
我尝试了很多方法,但无法达到结果。
我想按右上方的按钮左右滑动。代码如下:
class ViewController: UIViewController {
@IBOutlet weak var listView: UIView!
@IBOutlet weak var collectionView: UICollectionView!
@IBOutlet weak var collectionViewConstraintWidth: NSLayoutConstraint!
var IsOpenedList:Bool = false
var collectionViewWidth:CGFloat = 0
var collectionData = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"]
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
collectionView.delegate = self
collectionView.dataSource = self
let button1 = UIBarButtonItem(title: "Sepet", style: .plain, target: self, action: #selector(ViewController.openList))
self.navigationItem.rightBarButtonItem = button1
let layout = collectionView.collectionViewLayout as! UICollectionViewFlowLayout
collectionViewWidth = layout.itemSize.width
// let width = collectionView.bounds.width / 2
// layout.itemSize = CGSize(width: width, height: 100)
// layout.estimatedItemSize = CGSize(width: width, height: 100)
// layout.minimumInteritemSpacing = CGFloat(2)
}
@objc func openList(){
collectionView.widthAnchor.constraint(equalToConstant: collectionViewWidth/3).isActive = false
collectionView.translatesAutoresizingMaskIntoConstraints = false
listView.translatesAutoresizingMaskIntoConstraints = false
let layout = collectionView.collectionViewLayout as! UICollectionViewFlowLayout
if IsOpenedList {
// collectionView.widthAnchor.constraint(equalToConstant: 600).isActive = true
// Add Constraint
let constraintWidth = NSLayoutConstraint(
item: collectionView,
attribute: .width,
relatedBy: .lessThanOrEqual,
toItem: nil,
attribute: .notAnAttribute,
multiplier: 1.0,
constant: 320.0
)
collectionView.addConstraint(constraintWidth)
let a = collectionView.widthAnchor.constraint(lessThanOrEqualToConstant: 320.0)
a.priority = UILayoutPriority(rawValue: 750)
a.isActive = true
// listView.leadingAnchor.constraint(equalTo: self.view.layoutMarginsGuide.leadingAnchor, constant: collectionViewWidth/3).isActive = true
// listView.leadingAnchor.constraint(equalTo: self.view.layoutMarginsGuide.leadingAnchor, constant: collectionViewWidth).isActive = true
collectionView.widthAnchor.constraint(equalToConstant: collectionViewWidth).isActive = true
// collectionView.trailingAnchor.constraint(equalTo: listView.layoutMarginsGuide.leadingAnchor, constant: collectionViewWidth).isActive = true
//// listView.isHidden = true
//// listView.leadingAnchor.constraint(equalTo: collectionView.layoutMarginsGuide.trailingAnchor, constant: 8).isActive = true
// collectionViewConstraintWidth.constant = collectionViewWidth
// collectionViewConstraintWidth.isActive = true
//// collectionView.reloadData()
}else{
// listView.isHidden = false
listView.leadingAnchor.constraint(equalTo: self.view.layoutMarginsGuide.leadingAnchor, constant: collectionViewWidth/3).isActive = true
// collectionView.reloadData()
}
UIView.animate(withDuration: 0.5) {
self.view.layoutIfNeeded()
}
IsOpenedList = !IsOpenedList
}
}
extension ViewController: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return collectionData.count
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let padding: CGFloat = 4
let collectionViewSize = collectionView.frame.size.width - padding
return CGSize(width: collectionViewSize/3, height: collectionViewSize/3)
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CollectionViewCell", for: indexPath)
if let label = cell.viewWithTag(100) as? UILabel {
label.text = collectionData[indexPath.row]
}
return cell
}
}