How can I position a particular Item to "Left Top" corner using UICollectionView scrollToItemAtIndexPath? - scrolltop

I'm using UICollectionView with Custom Layout. In that whenever collectionview getting reloaded, I need {0,0} item should be positioned to Top Left corner of contentView. If I'm setting scroll position to "UICollectionViewScrollPositionTop" it will scroll {0,0} item to Top only but not to Left Top corner.
So, How to do that?
Thanks in advance!

Using contentOffset I had done this easily.
i.e. Every time before I reload UICollectionView I had been set the collectionView's contentOffset to CGPointZero without animation. It worked nicely! :-)

When using the UICollectionView scrollToItemAtIndexPath: method, then you can (bitwise) combine the scroll position indicators like this:
UICollectionView *collectionView = .....;
[collectionView scrollToItemAtIndexPath:indexPath
atScrollPosition: UICollectionViewScrollPositionTop | UICollectionViewScrollPositionLeft
animated:YES];
This way you can combine the x- and y-axis scroll position.
Hope this helps!

Related

Swift UITableView: How to remove space under the last cell

I have a normal UITableView with two cells. Every cell has a height of view.frame.size.height - 250 in the heighForRowAt method.
But under the last cell a white space appears. Look:
But I would like that it is like that:
Does anyone know how to do this?
Warm greetings
PS: The bottom constraint of my tableview is 0 = superview
Add tableview bottom constraint with superview rather than with Safe Area.
The issue is because of tableview bottom constraint with Safe Area.

UICollectionView: Animate cell while scrolling

I want to animate the top and bottom most cells in a UICollectionView as they enter and leave the screen. I don't just want to animate using scrollview delegates and a one time animation. I want the animations to be progress driven.
Example: Top most cell gradually fades away as cell scrolls farther offscreen.
I have scoured the internet for a way to do this, but the solutions are either way outdated or don't achieve this effect. Thanks in advance.
You could try to use NSCollectionLayoutSectionVisibleItemsInvalidationHandler
let section = NSCollectionLayoutSection(group: group)
section.visibleItemsInvalidationHandler = { visibleItems, scrollOffset, layoutEnvironment in
// perform animations on the visible items
}
If you know height of cells, you could track with scrollOffset when top cells will be scrolled away and add animations.
When a new item is added to visibleItems, you could apply to it animations too. Also if a new item is added, than the top one will be replaced, so you could work just with visibleItems for both cases.

Create a UICollectionView that scrolls with another UICollectionView

I have already built out 2 collection views, 1 horizontally scrolling (at the top) & 1 vertically scrolling (at the middle - bottom) that are used to view 2 different sets of content in my Objective-C iOS application similar to this Instagram screenshot:
I am trying to add functionality to make it so that the horizontally scrolling Collection View disappears when the user scrolls up on the vertically scrolling one. What is the best way to accomplish this task? I have looked up tutorials on adding a collection view in another collection view's cell but I cant find anything on just adding a collection view to the 1st cell of another collection view. What would be the best way to accomplish this functionality?
I think you should you use UITableView with UICollectionView. On the screeshot, I think horizontal collectionview is embedded in first cell of the tableview. And when use starts to scroll tableview, first row is gone as you want.
Edit
Create uitableview with 2 prototype cells. Create horizontall collectionview and embed it in first cell of tableview, this is first prototype cell. Then create second prototype cell for images. And when user will start scroll the tableview first cell will gone, as you want.
If you don't want to go with the other suggested method of embedding the horizontal collection view into the top cell of the vertical collection view, you could use the vertical collection view's scrolling callbacks (scrollViewDidScroll, since UICollectionView subclasses UIScrollView). When the vertical collection view scrolls, you can apply a transform to the top collection view to move it off the top of the screen based on the contentOffset of the vertical collection view, and then have it reappear once the contentOffset approaches 0.
Keep in mind that with this approach, the vertical collection view's frame will likely be the height of the screen minus the height of the horizontal collection view. Therefore, you will need a bit of extra logic to expand the vertical collection view's frame to take up the whole screen once the horizontal collection view has disappeared from sight. Otherwise, you will have an awkward blank bar at the top of the screen where the horizontal collection view initially was while you scroll.
You have two scroll view lets call it "cvHorizontal" and "cvVertical".
You can manage scrollViewDidScroll method to hide cvHorizontal when scrolled up and show cvHorizontal when scrolled down.
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
if (lastContentOffset > scrollView.contentOffset.y){
[self showCategory:YES];
lastContentOffset = scrollView.contentOffset.y;
} else if (lastContentOffset < scrollView.contentOffset.y) {
[self showCategory:NO];
lastContentOffset = scrollView.contentOffset.y;
}
}
-(void)showCategory:(BOOL)flag{
if(flag){
[UIView animateWithDuration:0.6 animations:^{
if(cvHorizontal.hidden ){
cvHorizontal.hidden=NO;
cvHorizontalHeight.constant=65.0f;//manage the cvHorizontal height and all the other constraints calculation if any
}
}];
}else {
[UIView animateWithDuration:0.6 animations:^{
cvHorizontal.hidden=YES;
cvHorizontalHeight.constant=0;
}];
}
}

Is there a way to get the position of the scrollIndicator in a collectionview?

I have a CollectionView with horizontal scroll direction in a UIView. I want to hide the scroll indicator and draw a line in its place. I am trying to retrieve the y coordinate of the scroll indicator so I can use it to position the line on top of the scroll indicator. How can get the y coordinate of the scroll indicator in my view?
a scrollview by default contains two subviews. the first is an imageview representing the horizontal scrollindicator and the second is an imageview representing the vertical scrollindicator. so:
let horizontalScrollIndicatorY = scrollView.subviews.first!.frame.origin.y
print(horizontalScrollIndicatorY)

How to draw connecting lines between two uitableviewcells ios

I want to show links between two cells of uiTableView.
For Ex:
To show links between cells 1 and 5, it could be shown like:
Does any one has any idea how this can be achieved. Also when table scrolls, these links should be scrolled with it.
This looks like you want to build hierarchical view. Your implementation might be rejected by Apple due to not taking HIG into account.
Also what will be in case when lower part is not seen to user? Arrow with no end and need to scroll down for the user?
You might want to do a tree like structure (anything hierarchical) instead of ugly (sorry for that) arrows.
If you want arrow between two cell then make a separate UIView class for the Tablecell, in that UIView add one UILabel for text and one UIImageView for arrow, adjust there position as per your requirement.
Now pass this UIView to cell.
Hope this will help you.
UITableViewCell is just a subclass of UIView and UITableView is a subclass of UIScrollView. The only fanciness that UITableView provides is creating/reusing the cells and laying them out in the scroll view. (That's a gross over-simplification but for this It'll do the trick.)
So if I have a UIView subclass that draws an arrow, then it's just a matter of getting the frame for the cells I want to point to. CGRect frame1 = [[self.tableView cellForRowAtIndexPath:indexPath] frame];
Some pseudocode...
topCellFrame = get top cell frame;
bottomCellFrame = get bottom cell frame;
arrow = new arrow view;
arrow set frame = frame with origin of top cell frame origin, and height of distance from topCellFrame to bottomCellFrame;
tableView add subview = arrow;
There are edge cases to think about, If the top cell or bottom cell are offscreen the cellForRowAtIndexPath: will return nil and frame will be CGRectZero.
But I'll leave that as an exercise for you.
Edit: (I haven't done this exact thing, but I have done some similar things with the frames of cells)

Resources