Block row and column CollectionView scroll - ios

And this is my code :
var collectionView: UICollectionView?
var collectionViewSeries: UICollectionView?
override func viewDidLoad() {
super.viewDidLoad()
// Year
let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
layout.sectionInset = UIEdgeInsets(top: 20, left: 10, bottom: 10, right: 10)
layout.itemSize = CGSize(width: 90, height: 120)
layout.scrollDirection = UICollectionViewScrollDirection.Horizontal
collectionView = UICollectionView(frame: self.view.frame, collectionViewLayout: layout)
collectionView!.dataSource = self
collectionView!.delegate = self
collectionView!.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: "Cell")
collectionView!.backgroundColor = UIColor.whiteColor()
collectionView?.scrollEnabled = false
self.view.addSubview(collectionView!)
// Series
let layoutSeries: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
layoutSeries.sectionInset = UIEdgeInsets(top: 20, left: 10, bottom: 10, right: 10)
layoutSeries.itemSize = CGSize(width: 90, height: 120)
layoutSeries.scrollDirection = UICollectionViewScrollDirection.Horizontal
collectionViewSeries = UICollectionView(frame: self.view.frame, collectionViewLayout: layoutSeries)
collectionViewSeries!.dataSource = self
collectionViewSeries!.delegate = self
collectionViewSeries!.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: "cellSeries")
collectionViewSeries!.backgroundColor = UIColor.whiteColor()
collectionViewSeries?.scrollEnabled = true
self.view.addSubview(collectionViewSeries!)
}
My question:
I'm trying to block the first column and the first row by scrolling.
So that only the central cells can flow.
This is an example of what I want to do:
link
Thanks

scrolling cannot be disabled for a particular cell. You create a UIView in Section Header similar to Cell 1. This will work as you like.
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *view = [[UIView alloc] init]; //your view
return view;
}

Related

CollectionView does not scroll when added to view

Here is my code:
let layout = UICollectionViewFlowLayout()
layout.minimumInteritemSpacing = .greatestFiniteMagnitude
layout.minimumLineSpacing = 15
layout.scrollDirection = .horizontal
layout.itemSize = CGSize(width: screenWidth*0.2, height: screenHeight*0.15)
collectionView = UICollectionView(frame: CGRect(x: 0, y: 0, width: screenWidth*0.9, height: screenHeight*0.15), collectionViewLayout: layout)
collectionView?.layer.zPosition = 10
collectionView?.register(PlantSnapshotCell.self,forCellWithReuseIdentifier:PlantSnapshotCell.identifier)
collectionView?.backgroundColor = .clear
collectionView?.alwaysBounceHorizontal = true
collectionView?.bounces = true
collectionView?.showsHorizontalScrollIndicator = false
collectionView?.dataSource = self
collectionView?.delegate = self
scrollView.addSubview(collectionView!)
view.addSubview(scrollView)
It renders in the view, but any sort of interaction does not work. The width of the collectionView is 90% the width of the superview. Scrolling horizontally does not work.
Why you are adding collectionView as a subview to the scrollView?

Swift - Change UICollectionViewCell size programmatically

I am struggling to change the size my items in the CollectionView.
Custom Flowlayout class found here:
This is my CollectionView:
class ExampleViewController: UIViewController, UICollectionViewDataSource {
let theCollectionView: UICollectionView = {
let v = UICollectionView(frame: CGRect.zero, collectionViewLayout: UICollectionViewFlowLayout())
v.translatesAutoresizingMaskIntoConstraints = false
v.backgroundColor = .white
v.contentInsetAdjustmentBehavior = .always
v.layer.cornerRadius = 30
return v
}()
let columnLayout = FlowLayout(
itemSize: CGSize(width: 50, height: 50),
minimumInteritemSpacing: 10,
minimumLineSpacing: 10,
sectionInset: UIEdgeInsets(top: 20, left: 20, bottom: 10, right: 20)
)
I tried changing the itemSize but that doesnt do anything.
You didn't assigned you flowLayout to the collectionView either try:
theCollectionView.collectionViewLayout = columnLayout or
First define your layout :
let columnLayout = FlowLayout( ...
Then define your collectionView like this:
let theCollectionView: UICollectionView = {
let v = UICollectionView(frame: CGRect.zero, collectionViewLayout:columnLayout)
Here is Code for CollectionViewCell Size Change
(Note:- Change values according to your Requirement!)
let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
layout.sectionInset = UIEdgeInsets(top: 20, left: 0, bottom: 10, right: 0)
layout.itemSize = CGSize(width: screenWidth/4, height: screenWidth/4)
layout.minimumInteritemSpacing = 0
layout.minimumLineSpacing = 0
cell2.collectionView2.collectionViewLayout = layout

Can't change CollectionView frame with CGRect

I am trying to build a simple side menu using CollectionView. The problem is that I can't get it to work because changing frame using CGRect doesn't seem to do anything and the menu won't appear. Any help would be much appreciated.
let blackView = UIView()
var collectionView: UICollectionView {
let layout = UICollectionViewFlowLayout()
let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
cv.backgroundColor = UIColor.white
return cv
}
func showMenu() {
if let window = UIApplication.shared.keyWindow {
blackView.backgroundColor = UIColor(white: 0, alpha: 0.5)
blackView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(handleDismiss)))
window.addSubview(blackView)
window.addSubview(collectionView)
//This line doesn't work
collectionView.frame = CGRect(x: 0, y: 0, width: 700, height: window.frame.height)
//This works fine
blackView.frame = window.frame
That's because the collection view here:
window.addSubview(collectionView)
and the collectionView here
//This line doesn't work
collectionView.frame = CGRect(x: 0, y: 0, width: 700, height: window.frame.height)
are different objects. You add the first collectionView to the window, and then you change the frame of the second one. The misunderstanding comes from this code:
var collectionView: UICollectionView {
let layout = UICollectionViewFlowLayout()
let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
cv.backgroundColor = UIColor.white
return cv
}
This creates a dynamic variable, so that whenever you access collectionView, a new UICollectionView is created. It's actually equivalent to a function call. It's like
func makeCollectionView() -> UICollectionView {
let layout = UICollectionViewFlowLayout()
let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
cv.backgroundColor = UIColor.white
return cv
}
and then later doing
window.addSubview(makeCollectionView())
//This line doesn't work
makeCollectionView().frame = CGRect(x: 0, y: 0, width: 700, height: window.frame.height)
What you should do instead is define the variable at the beginning, and initialize it later. For example like this:
let blackView = UIView()
var collectionView: UICollectionView!
func showMenu() {
if let window = UIApplication.shared.keyWindow {
blackView.backgroundColor = UIColor(white: 0, alpha: 0.5)
blackView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(handleDismiss)))
// Create collectionView
if collectionView == nil {
collectionView = UICollectionView(frame: .zero, collectionViewLayout: UICollectionViewFlowLayout())
collectionView.backgroundColor = UIColor.white
}
window.addSubview(blackView)
window.addSubview(collectionView)
//This line works finally
collectionView.frame = CGRect(x: 0, y: 0, width: 700, height: window.frame.height)
//This works fine
blackView.frame = window.frame
(depending on your code setup, you may have to create the collectionView at another place, maybe in viewDidLoad or something like that (I'm not sure))
You define the wrong lazy var as the following:
var collectionView: UICollectionView {
let layout = UICollectionViewFlowLayout()
let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
cv.backgroundColor = UIColor.white
return cv
}
In this situation, the following is the correct format which means its READONLY. you cannot change after return.
var collectionView: UICollectionView {
get{
let layout = UICollectionViewFlowLayout()
let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
cv.backgroundColor = UIColor.white
return cv}
}
So if you want to change frame, you should change before return like this:
var collectionView: UICollectionView {
get{
let layout = UICollectionViewFlowLayout()
let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
cv.backgroundColor = UIColor.white
cv.frame = CGRect(x: 0, y: 0, width: 300, height: 300) // just an example.
return cv}
}
What you Really want should be like this and define it as a computed property, then you can change frame at anywhere:
var collectionView : UICollectionView = {
let layout = UICollectionViewFlowLayout()
let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
cv.backgroundColor = UIColor.white
return cv
}()
So, basically you just miss a "=" and "()". Hope you know the reason now.

UICollectionCell position issue

CollectionView cell is not coming at correct position. I am using following code to create the Collection view
let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
layout.sectionInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
layout.scrollDirection = UICollectionViewScrollDirection.Horizontal
viewListCollection = UICollectionView(frame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height), collectionViewLayout: layout)
layout.headerReferenceSize = CGSizeZero
layout.footerReferenceSize = CGSizeZero
viewListCollection.dataSource = self
viewListCollection.delegate = self
viewListCollection.backgroundColor = UIColor.whiteColor()
viewListCollection.tag = 9999
viewListCollection.registerNib(UINib(nibName: "SelectedContactCell", bundle: nil), forCellWithReuseIdentifier: "SelectedContactCell")
viewListCollection.scrollEnabled = true
viewListCollection.collectionViewLayout = layout
self.addSubview(viewListCollection)
blue colour is of CollectionView and yellow colour is of CollectionViewCell . You can see the top distance of the cell, why this space is coming.And how to remove that?
Try this in your viewDidLoad:
self.automaticallyAdjustsScrollViewInsets = false

UICollectionView Magic Selection

I added collection view and add selection events:
private func addFriendCollectionView() {
let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
let cellSize = (mainContainerView?.frame.width)! / 4
layout.sectionInset = UIEdgeInsets(top: 10, left: 10, bottom: 50, right: 10)
layout.itemSize = CGSize(width: cellSize, height: cellSize)
layout.minimumLineSpacing = 50.00
let positionRectangle = CGRect(x: 5, y: 230, width: (mainContainerView?.frame.width)!, height: 300)
friendsCollection = UICollectionView(frame: positionRectangle, collectionViewLayout: layout)
friendsCollection?.registerClass(FriendCollectionViewCell.self, forCellWithReuseIdentifier: "friendCell")
friendsCollection?.backgroundColor = UIColor.whiteColor()
friendsCollection!.layer.borderColor = UIColor.grayColor().CGColor
friendsCollection!.layer.borderWidth = 0.5
friendsCollection?.tag = 1
friendsCollection?.allowsMultipleSelection = true
friendsCollection?.dataSource = self
friendsCollection?.delegate = self
self.view.addSubview(friendsCollection!)
}
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
if collectionView.tag == 1 {
let cell = collectionView.cellForItemAtIndexPath(indexPath) as! FriendCollectionViewCell
selectedFriends[indexPath] = cell.friendName.text
cell.selectBlackView.hidden = false
cell.selectedImage.hidden = false
print("\(selectedFriends.count) - \(indexPath)")
} else if collectionView.tag == 2 {
let cell = collectionView.cellForItemAtIndexPath(indexPath) as! DateSelectionCollectionViewCell
print("\(cell.weekday.text) and \(indexPath)")
}
}
But selection layer add every cell with offset 9. If i am select 1 selected 10, 19, 28... etc but indexPath change correct

Resources