iOS Swift UICollectionViewCustomLayout scrollDirection - ios

i'm trying to change UIColloectionViewCustomLayout scrollDicretion to Horizontal. I have tried to :
let layout = CollectionViewLayout()
layout.scrollDirection = .Horizontal
self.collectionView.collectionViewLayout = layout
I need get this results
[1][4][7]
[2][5][8]
[3][6][9]
Now i have this
[1][2][3]
[4][5][6]
[7][6][9]

Here is a few links to help you understand what to do.
http://www.skeuo.com/uicollectionview-custom-layout-tutorial
https://www.amazon.com/iOS-UICollectionView-Complete-Mobile-Programming-ebook/dp/B00CFLTD50?ie=UTF8&redirect=true
On iOS, can you use UICollectionViewFlowLayout to get a vertical column layout?
UICollectionView - Horizontal scroll, horizontal layout?
Reorder cells of UICollectionView

Related

Get default scroll direction for UIScrollView Swift

I have an application that uses more UIScrollViews and extensions from it like UICollectionViews and UITableViews and for a little dynamism I have created some general functions to help me with some paddings / margins.
My functions are created as extensions for UIScrollView and I want to use them just when the scroll view is only vertical.
My Question
How can I find the scrolling direction of a UIScrollView?
I have found a lot of answers of how to find the scrolling direction, but I need when the UIScrollView is initialised to find the scrolling direction of it, if it's vertical or horizontal.
Thank you for your time!
One solution I can think of in order to get the scroll direction before scrolling is to react accordingly to the type of UIScrollView in question.
So first step check if it is a UIScrollView, UICollectionView or UITableView.
In the case of a UITableView
I think a fair assumption here is that the scrolling direction will be vertical
In the case of a UIScrollView
This one could be tricky but I guess here you would have to compare the content size of the scroll view with the frame of the scrollview as Desdenova suggested and this can suggest if you can scroll horizontally and / or vertically.
In the case of a UICollectionView
This data will lie in the in the layout of the UICollectionView.
For example, I set up a basic UICollectionView in Storyboard using a FlowLayout without any data in it.
Then I add this code somewhere appropriate
if let layout = collectionView.collectionViewLayout as? UICollectionViewFlowLayout
{
if layout.scrollDirection == .horizontal {
print("horizontal")
}
else {
print("vertical")
}
}
This prints out horizontal.
For the UIScrollView you have to use the UIScrollViewDelegate scrollViewDidScroll which is helps you in UIScrollView's Scroll direction also for that you have to add some checks for the get the directions:
func scrollViewDidScroll(scrollView: UIScrollView!) {
// Update ScrollView direction
if self.lastContentOffset.y > scrollView.contentOffset.y && self.lastContentOffset.y < scrollView.contentOffset.y {
// Vertical Scroll
} else {
// Horizontal Scroll
}
}

UICollectionView first cell not aligned properly

In my app, I have a UICollectionView inside a UITableView that is showing cells to select a day.
I had a weird issue where sometimes, the cells would be centered correctly, and sometimes it wouldn't, to the point where part of the first cell is cut off in the view.
Normal
Not Aligned
What is going on? I've adjusted the UIEdgeInsets, but it doesn't seem to make an effect.
I have found out that my issue was fixed when I removed this line of code for the UICollectionViewFlowLayout of the UICollectionView
For a defined
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
Remove this
layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;

How to make uitableview to fill all my view?

I am very new to iOS. Can anyone tell me how to fix this, to fill all my view with UITableView:
I am trying to do it with auto layout and constraints but I don't know why isn't working.
What you want to do is to have the cells at equal height so that all cells together fit exactly the height of the screen.
You cannot do that with auto layout. You have to tell the table view the height you'd like the rows to have like so:
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
let heightOfVisibleTableViewArea = view.bounds.height - topLayoutGuide.length - bottomLayoutGuide.length
let numberOfRows = tableView.numberOfRowsInSection(0)
tableView.rowHeight = heightOfVisibleTableViewArea / CGFloat(numberOfRows)
}
Note that this code assumes that you don't implement the method tableView(_:heightForRowAtIndexPath:).
To fill the whole view, with a table view, I use four constraints:
One. tableView centre = view centre.
Two. tableView width = view width.
Three. tableView vertical distance to top layout guide = 0
Four. tableView vertical distance to bottom layout guide = 0.
To fill everything with the same color, I set the backgroundColor of the tableView, then set the backgroundColor of the cells to [UIColor clearColor].

Can you amend UICollectionView scrollDirection programmatically?

I have a UICollectionView that is created using Storyboard (given it has quite complicated cells). I want to programmatically change the scrollDirection to be horizontal (on 4 inch screens) and vertical (on 3.5 inch screens).
I see you can set scrollDirection upon initiation, but I cannot see how you can access this property from an already created UICollectionView.
Does anyone know if this is possible?
What you are looking for is the
UICollectionViewFlowLayout
This is how you would go about using it
UICollectionViewFlowLayout *horizontalLayout = (UICollectionViewFlowLayout *)self.collectionView.collectionViewLayout;
horizontalLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
[self.collectionView setCollectionViewLayout:horizontalLayout];
I assume that your collectionView is linked with the storyboard as self.collectionView
This is the quick and dirty way. What you should do is create your own layout and subclass the UICollectionViewFlowLayout class.
An example can be found here: UICollectionViewFlowLayoutExample by AshFurrow

Can UICollectionView count horizontally?

I have this from a programmatically implemented UICollectionView:
Is there a way (programmatically) to ask it to count left to right instead of top to bottom so that there would be two rows of four instead of two columns? Thank you!
If you are using a flow layout you can set the UICollectionViewFlowLayout's scrollDirection property to UICollectionViewScrollDirectionVertical:
UICollectionViewFlowLayout *layout = collectionView.layout;
layout.scrollDirection = UICollectionViewScrollDirectionVertical;

Resources