UICollectionView - a couple of questions - ios

I'm working on an app that is making use of a collecitonview. It's pretty simple as far as the colletionview goes...just displays an array of uiimages in a single-line, horizontal direction view. Here are my questions...
Do I need to have a custom flow layout just to center the images when scrolling? I was able to center the first image the way I want by using insetForSectionAtIndex, however, when I scroll (horizontally), the images are aligned to the left edge of the screen. I was able to fix that by subclassing UICollectionViewFlowLayout and overriding targetContentOffsetForProposedContentOffset. However, I think there has to be a way to do such a simple task without using a custom layout. Also, using this method, while it centers the images when scrolling, it does not allow paging (no snapping the next image in place), which I would prefer. I thought that insetForSectionAtIndex would be called when scrolling through each image, but that does not seem to be the case. Is there any way to force that?
The other question is sort of related to the first...how do I place the cells exactly where I want them? I have the images centered as far as the left and right edges, but I need to move the cells up (- negative) on y axis. Is overriding layoutAttributesForElementsInRect the only way to do this?
Thanks in advance!

In the storyboard, after you add the imageView to the contentView of the cell, use this to center it:
imageView.center = CGPointMake(cell.contentView.bounds.size.width/2,cell.contentView.bounds.size.height/2);

Related

UICollectionView Full Screen Vertically and 2 Screens Horizontally

I have a UICollectionView that displays one cell at a time that has the size equals to the UIViewController.view.bounds.size. This works great for me on Vertical/Portrait screen.
I want to rotate the screen to landscape and a nice animation happens to show 2 cells side by side each having size equals to CGSize(UIViewController.view.bounds.width / 2, UIViewController.view.bounds.height)
Is there a library that does that or how can I achieve this approach?
You can think of it as an image gallery that displays one image at a time when rotated should show 2 images.
If you are using flow layout, then all you have to do is implement UICollectionViewFlowLayout delegate methods, and invalidate the collectionView's layout on rotation.
Take a look at this repo: dynamic width cells
It's not exactly what you are looking for, but you can get a general idea.

Custom Collection View Layout like Chanel app

I'm trying to do a custom layout like the Chanel app you can find the app in the Appstore.
https://itunes.apple.com/us/app/chanel-fashion/id409934435?mt=8
I know they are using an UICollectionView, but no clue how to start.
The interaction feels like a tableview mixed with a paginated scroll. When you scroll the elements grow, and the first element position itself at the top.
Start with dragging & positioning just one UIView. See UIGestureRecognizer docs and look for existing examples of movable views. You'll need an UIPanGestureRecognizer to move the view.
Resize the view depending on its Y position.
Create & position an image inside that view depending on the view size using a couple autolayout constraints.
Note that Chanel app has different constants for these constraints. With a minimum view height, one image's top is 80% height, for another image it's 90% height. Make sure you can manipulate constraints from code (I think it's a good idea to create everything from code there, XIBs are not very flexible).
Make the view "anchoring" to certain points (e.g. top = -75%, 0%, 75%, 90% from what I see in the Chanel app) when you stop moving it. Just find the nearest one and animate the view to it.
Once you did it with 1 view, move all your work to an NSView subclass (if it's not yet there) and create a collection of these views.
You can create UICollectionView, but I'd rather do it with a simple NSArray: I actually don't see a reason to use UICollectionView here; it's a very simple structure. Anyway, you write your own gesture recognizer (don't you? I can't see another way) - so what's the point to use UICollectionView? If you want to expand the app functionality some day, UICollectionView will unlikely help you with that. Well, it's my hypothetical approach, you can find another one while working on that.
Position other views while you're moving an "active" view. Do it by hand, without any UIScrollViews.
Write a function that reflects the Y position of the "neighbor" views while you moving one. It should "slow down" to the bottom of screen.

Randomly sized UICollectionViewCell circle design

I want to have a collection view with randomly sized cells but each cell is circle. An example mockup is shown below.. The question is
how would I go about doing this?
Edit: The design doesn't have to be a UICollectionView, I just assumed this would be the best way to do it.. I'm definitely open to any way about doing this.
I also think using a UICollectionView is a good idea. You would definitely have to implement a custom flow layout to be able to organize these random sized cells. Maybe taking a look at CCHexagonFlowLayout and MosaicLayout could help. As for drawing the circles inside the cells, you could have a square UIView in the cell and set circleView.layer.cornerRadius to half of its height.
The collection view is a pretty good idea. Using the flow layout, you can implement collectionView:layout:sizeForItemAtIndexPath:. Answer a random square size (see arc4random_uniform()).
Within each cell, inscribe a circle. The simplest way to do that (at least for a start) is a UIImageView whose frame fills the cell bounds and whose content mode is set to UIViewContentModeScaleAspectFit. All you need then is a picture of a circle.

Simple way to stagger cells in UICollectionView

I am working on an iOS app that has a UICollectionView. I would like to make a simple custom layout in which the middle cell on each row is slightly lower than the one on the right and left. I've been looking over tutorials and the apple documentation but most of it is for a much more complicated set up.
Is there is a quick way to stagger the middle cell. Thanks for any input!
Currently the collectionView looks like this...
I simply want the center cells shifted down by 40 pixels or so.
Subclass UICollectionViewFlowLayout and alter the response that the superclass gives so that the cells are positioned where you want them.
Here's an example of a UICollectionViewFlowLayout subclass:
https://github.com/mattneub/Programming-iOS-Book-Examples/blob/master/bk2ch08p466collectionViewFlowLayout2/ch21p748collectionViewFlowLayout2/MyFlowLayout.swift
It shifts the cells left (so that they are left-justified instead of full-justified across the screen). It is not difficult to see how to adapt this to shift certain cells down.

IOS Prevent text from stacking on screen rotation

I have a custom table view cell with two labels in it. If I rotate the screen the two labels stack on top of each other and behave badly. If I back out and display the view again while still in landscape mode they appear correctly.
I have figured out how to prevent this between a label and a textfield or a webview by pinning them with horizontal spacing, but that doesn't seem to be possible with two labels. Any suggestions would be much appreciated.
You may need to intercept the viewcontroller rotation delegate callbacks and handle them how you want by manually positioning labels on the screen.
I think all views should be re-positioned again in viewWillLaypitSubviews or viewDidLaypitSubviews methods. Otherwise, the resizing decision is put to iOS. If you don't want a resize, you can try playing with the autoResizingmask and autoResizeSubviews properties of the table view cell.

Resources