I am trying to achieve specific layout inside UICollectionView.
I need to adjust my phone layout to work well with iPad, so I used horizontal scrolling direction for "default" flow layout.
But unfortunately I am doing something wrong. Please look at following sketch:
These are elements view:
So far, I will not have too much cells, but maybe they will not fit (so I need to use UIScrollView derivate).
One cell is double in size (plus one margin space), a very first one.
Other cells are all same size (square)
As can be seen in sketch, row is not "breaking" in right moment (even after I played with contentSize) and 1st cell in 2nd row occupy two places.
If nothing helps, I will start with writing own UICollectionLayout descendant, but maybe it's something simple and can be done with "standard" flow layout.
Thank you.
Sample file
I needed to subclass UICollectionView.
I have followed tutorial from Raywenderlich, and converted vertical scrolling from their example to my horizontal layout.
I avoided using custom layout for long, but luckily it's not too bad at the end.
Related
I'm stuck with a problem I'm sure one of you guys can help me with.
I'm developing an iOS-Application with Swift3 and what I'm trying to create is something like this:
Screenshot1
Screenshot2
The screenshots show just a part of my UIViewcontroller. The remaining space is filled with other views that are not part of the question.
As you can see the size and position of my subviews(the icon) depends on the amount of icons i'm given (not more than 8). If more than 5 icons exist it should create a second row and decrease the size of the icons to fit the space. My views should always be centered meaning they are evenly distributd to the left and right.
What i tried so far:
I created a horizontal StackView and tried to fill it programmatically but that did not solve the problem for more than 5 Views.
Tried setting up some views with auto-layout constraints but could not solve the problem of different sizes and positioning.
Of course I could do everything in code. Creating and placing all Views one by one but I would like to find a cleaner solution.
Thanks for you answers. Feedback is appreciated.
UICollectionView Can be a better option here.
Its either UIStackView or UICollectionView since they both handle the distribution of subview. Since you have a max of 8 icons I'd just use UIStackView and vertically stack two horizontal stackViews. You put items 1-5 in stackview 1 and 6-8 in stackview 2. If you need the two rows to have the same distribution then you need to put empty views with horizontal constraints equal to the imageView width to make the second stackview have as many items as the first when the number is odd.
Thanks to Shuja and Josh for leading me in the right direction.
I ended up using a UICollectionView and changing some values inside sizeForItemAt to respond to changing cell sizes and insetForSectionAt functions to center my cells horizontally.
I have two different problems with my Static UITableView.
1. The height of the table:
I have 4 cells in my table, the fourth one is going out from the screen size.
(the table are bigger than the screen).
how can i allow scrolling to the size of the table?
2. Scrolling Horizontal
I don't know why, but when I run the app, I can scroll horizontal the all table to the left side.
Why does it happen? how can I fix it?
thank you!
Set top,bottom,leading and trailing constraint from the interface builder (i.e. storyboard) to your tableview. Something like below screenshot,
In the screenshot constrains got setting for address label, you need to select your tableview and need to set four constraints.
If you totally unaware of autolayout then first learn it. refer some tutorial like : Raywenderlich's tutorial or Appcoda's tutorial.
I've been trying for days to make one layout of my app to work well, and after days of learning and mistakes I still can't get the table cell layout to look how I want it to be.
This is how my cell .xib looks like in the editor:
http://i.stack.imgur.com/VEr3r.png
And this is how my app looks like when running with suggested constraints:
http://i.stack.imgur.com/wiK1f.png
Why is that? How I can find my mistake and make the layout like it supposed to be, in the view?
Suggested constraints are rarely what I actually wanted to see.
For each label with fixed text, lock the horizontal and vertical positions either to the view or to the next adjacent item.
For imageViews, choose the size you want and lock the height and width.
For labels that you will be dynamically changing the text on, pick a size that will hold the longest string and lock the width. You'll need a vertical position constraint.
You have many options to simplify your layout.
you can use the stakview or create a static UITableView and insert your component inside the cells and enter the Constraint. excellent tutorial http://www.runtimecrash.com/2015/09/17/exploring-uistackview/
I've seen a lot of different topics and suggestions on aligning and inputting buttons/text, but the ways I've seen seem kind of risky.
What is the optimal way, for example, to add two buttons, stack them together, and have them be 10% from the bottom of the screen, and centered horizontally on all devices?
Learn Auto Layout if you haven't yet. Use constraints for achieving the following:
For centrally Horizontal on all devices: Use Center X with SuperView.
For having them 10% from bottom, use multiplier value say 0.10 .
The optimal way would be using storyboard for implementation and use of constraints in a proper way.
For example, as you suggested you want bottom space to be 10% of device height so it can be done with the multiplier in constraints also the horizontal center can be easily done with the same, so you could look for a good tutorial of Auto Layout and constraints for better understanding.
Use your Storyboard and add Auto Layout to your elements. Here is an example:
I have added auto layout
Left
Right
Top
Bottom
If I want to change the distance between the buttons, I just choose the auto layout constraint that I have created between the buttons and changes the constant value to it.
If I say that "Button 4" constant to the top shall be 40, then it´s always 40 no matter what size the phone has. If I change the constant between the buttons to 10 then it´s the same for all sizes.
Your best bet is to use Auto Layout. It takes a bit of learning but once you get used to it you can add constraints pretty quickly and easily in Interface Builder. I can't recommend any particular guide but there are a lot of good ones to be found with a quick Internet search.
Here is an example of constraints that seem to be what you are looking for:
For iOS 9, an even simpler Auto Layout approach would be to use UIStackView.
As you can see, no constraints are needed for the buttons embedded in the stack view, as the stack view lays out the buttons for you. All you have to constrain is the location of the stack view itself.
Here's an example of two vertically stacked buttons, 10% from the bottom of the screen, and centered horizontally for all devices.
I have a grouped UITableView that was really designed to look nice in portrait mode for iPhone. Its cell subviews have autosizing set up so that they stretch in landscape mode, but this makes it a lot less aesthetically pleasing -- the cells just look too wide for their content.
I'm now making it a universal app but on iPad the autosizing causes even more stretching and it looks just unacceptable.
It would be ideal if I could make the UITableView's groups of cells have a fixed width (or a max width), or if I could somehow control the horizontal margins.
Having not found support for this in UITableView, I have done a few quick attempts at subclassing it to constrain its size at layout time and, as an alternative, at introducing a container view in order to make the UITableView autoresize vertically only. Both approaches work but create new problems: Scrolling doesn't work when swiping in the margins, and I am now forced to make the UITableView's background transparent (which goes against Apple's recommendations) as there is now a discontinuity of background between the UITableView's frame and the margins.
Has anyone found a trick to solve my problem (i.e. constrain the width of the groups in a UITableView, causing margins to expand to fill the width of the view), or an open source solution to it?
Good news! I finally found a way to achieve this satisfactorily with only tiny code changes:
Shrink the cells by subclassing UITableViewCell and overriding -setFrame, as per the solution to this post:
How to set the width of a cell in a UITableView in grouped style
And to add vertical padding, using the contentInset property of the UITableView (inherited from UIScrollView) works pretty well.
You can always keep the standard table view and provide custom backgrounds with transparent sides for the table view cell's so that they look smaller than they are.
Cocoa With Love has a great article on how to do that here: Easy custom UITableView drawing.
The basic gist of the article is that you need to make six different versions of the backgrounds, and supply the correct one when tableView:cellForRowAtIndexPath: asks for a cell. You will need one with rounded corners at the top (for the first row of a section), one with rounded corners at the bottom (for the bottom row of a section), and one with all four corners rounded (for when there is only one row in the section). Then you will need the same three, but customized for the "selected" version of each row.