How to make a grid layout in iOS? - ios

I want to make a kind of grid layout (not scrollable) in an iOS app, similar to this:
I can do it using multiple UIView but that won't be a good solution. If I use `UICollectionView', I believe it will become scrollable? I want fixed grid items, which resize them according to screen size.
Any guidelines please?
Thanks!

You should use collection view with scrolling and bouncing disable.
It is all about your collectionview's flowlayout's item size.
You have to calculate item size receptively your screen's size.
And you can set that item size to flowlayout and can use that flowlayout with your collection view.
For example, You can refer this so post.

If you just want fixed 3x2 layout scaling to any screen size then multiple UIView is a good solution for this and exactly what you should do.
Also make a RestaurantView UIView subclass to configure and display the image/rating/title/description/etc details for each restaurant.
-
If you want the number of restaurants to change depending on screensize then use a UICollectionView with ResturantCollectionViewCell instead.

Related

how to show my custom collection view for other devices same

I created a collection view custom cell like this:
on iPhone 6.
But when I run my app on iPhone 5 or 5se my cell is like this:
How can I fix this?
If you want a decent amount of control over the layout of your UICollectionView you should have your viewController (or whoever is the UICollectionViewDelegate) also conform to UICollectionViewDelegateFlowLayout. This will allow you to set cell size, margins, spacing between items, etc. It is a very easy way to make the cells dynamically sized or spaced. See Apple's docs for more specifications.

Make UICollectionViewFlowLayout not centering cells on the same line

According to Apple Documents:
If you want to specify different sizes for your cells, you must implement the collectionView:layout:sizeForItemAtIndexPath: method on the collection view delegate. You can use the provided index path information to return the size of the corresponding item. During layout, the flow layout object centers items vertically on the same line, as shown in Figure 3-2. The overall height or width of the line is then determined by the largest item in that dimension.
https://developer.apple.com/library/content/documentation/WindowsViews/Conceptual/CollectionViewPGforIOS/UsingtheFlowLayout/UsingtheFlowLayout.html#//apple_ref/doc/uid/TP40012334-CH3-SW1
Now I have a collection view with 2 columns and cells varying in height. The problem is that the smaller cell is centered with the taller cell on its left/right, but not float up with the cell under it floats up too.
How can I make that happen?
What I have:
What I want:
It seems like that I should subclass UICollectionViewFlowLayout, but how should I accomplish this?
The type of collection view layout you desire is called "waterfall layout". The implementation is a little bit tricky, since you need to override the basic behaviour of UICollectionViewFlowLayout.
I suggest you to take a look at this tutorial by Ray Wenderlich for building a waterfall layout from the ground, or - if you desire an already packed library - using a library on GitHub, like CHTCollectionViewWaterfallLayout, WaterfallCollectionView or CollectionViewWaterfallLayout
What you want to achieve is called "staggered layout". It was introduced by Pinterest in their iOS app.
You will need custom collection view layout, this tutorial will explain how. Alternatively, you can use one of the existing solutions, for example CHTCollectionViewWaterfallLayout.

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.

hide/ show controls in View controllers

My application gathers input from users and hence it is full of Labels, text boxes and buttons and I have to show or hide set of labels and text boxes based on certain conditions.
To accomplish it, I did the following.
Set fixed height (lets say 30) for all the controls
Set height constraint on each of the controls and created an outlet to the height constraint in the ViewController
Alter the heightConstraint.constant value programatically (between 0.0 and 30.0) based on the scenarios.
Having programmed like this, it is very difficult for me if there is any change in layout. (i.e., if user requested to add/remove any particular control from the view Controller).
I am using Auto Layout constraints. Could anyone suggest if there is a better way to accomplish my goal.
I am using IOS9 and Swift.
Many thanks in advance.
You can use UITableViewController with static cells for this case.
For hide/show row or section, you can change the size in the tableView:heightForRowAtIndexPath method.
UITableViewController automatically manages the layout and with static cell you can even create outlet for all the controls.
Have you considered using a table for this? It has mechanisms for inserting and deleting rows and would manage the layouting for you - only part you'd need to care about are the contents of the cells.
Instead of making IBOutlets to the height constraints of all the views that you might need to hide, you can just use the hidden property of UIViews to hide/show the view.
In case you need the view to make space for other views, you could set a simple animation and move the view out of screen bounds. You might face issues with layout constraints but it's surely worth the effort from a UI/UX perspective.
Additionally, if you know that you don't need a view any more you can even remove the view from it's superview.

UITableView with dynamic height in Swift

I would like to create a dynamically sized UITableView in Swift.
When I add a UITableView via the storyboard some content is cut off at a certain point.
Is there a way to make it possible to scroll through the content without cutting off any of it? I've thought about adding a UIScrollView, but this kind of view seems to be used for something else (e.g.: http://www.raywenderlich.com/76436/use-uiscrollview-scroll-zoom-content-swift )
At best I'd like to have a scrollable UITableView that almost fills the screen and a button that always stays on the very bottom.
Edit - To clarify what I meant by cutting off: When there are e.g. 100 elements in an array, only 10 were displayed.
But the concept of placeholder constraints solved this issue, since my array length is determined during runtime.
To have a dynamically sized UITableView, use AutoLayout. https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/AutolayoutPG/Introduction/Introduction.html
Yes its possible to not cut your content, but how is your content cutted ? Could you link a screenshot about it ?
You didn't talk about your constraints, did you set yours correctly ?
For example, you can set them like that for :
UITableView:
UIButton:
UITableView is a subclass of UIScrollView, your link show a specific usage of UIScrollView options (Paging), but it is not the default behavior of this component.
Here is an example: http://spin.atomicobject.com/2014/03/05/uiscrollview-autolayout-ios/

Resources