I'm showing some photos with a NSCollectionView in FlowLayout. This works fine. Then I wanted to use dynamic width for each item for better distribution of the images when some are landscape and some are portrait.
So I implemented the collectionView(_:layout:sizeForItemAt:) to return sizes.
And now I have a strange behaviour on rows with less items then is space available. Sometimes the lines are filled from left to right with great spacing between the images (justify) and sometimes they are just half filled rows with the defined minimum item spacing (left aligned).
I like the left aligned version, but reading the docs the justified version should be the correct one. But why it is randomly sometimes this and sometimes the other way?
How can I do something against it?
Do I have to implement my own custom flow layout, or is there a explanation and solution for this strange behaviour?
Mininum spacing and margins are set static, only the cellsize is dynamically. And not using dynamic cell size it just works fine.
Related
A designer I'm working with wants to have a UIImageView be at a specific y position, with a specific width/height depending on the iphone size. I've done research and I haven't been able to find anything online that allows me to do so with constraints on the story board. I want to avoid having to use code because throughout the rest of the app there are a lot of similar situations. In the past I've made different storyboards for each phone size to accommodate for the specificity our clients request, but I want to avoid doing this because it feels like bad practice. Is this even possible?
This can be completely handled in the Interface Builder. Follow the steps below.
1) Add your UIImageView to the ViewController
2) Set constraints for Width, Height, Vertically Align and Horizontally Align
3) You can adjust the Y position in the Size Inspector of the view.
4) Now switch to Assistant Editor and choose 'Preview' and add different screen sizes to see the auto layout in action.
All of this will set a static image size of 250x200, horizontally centered, and vertically at 1/4 of the screen height.
Little-known capability of constraints are that you can set view positions, or in your case sizes, by way of ratios against other views.
With just your image view selected, the tie-fighter :) constraints button lets you specify absolute positions & sizes: do that to set your desired y-position. But then select both your image view and its enclosing parent view, then the pop up for that same button now enables the equal height/width buttons. Add those constraints and then edit the constraint to change the 1:1 ratio to whatever ratio you need.
Excuse my lack of screenshots, posting from my phone.
I've been playing around with the auto-layout feature of XCode 6 in developing a Swift application for the iPhone.
It works well for a small number of controls but I now find myself with a section on the screen which will consist of a matrix of eight by eight small text controls (in portrait mode) of the form 7☐ and 42☑ (all three characters), each which can be selected by the user (toggling the checkbox).
I'm wondering what's the best way to handle this. I started by simply laying out all sixty-four text controls but this quickly became a nightmare in trying to join them all together with constraints.
I then wondered whether it would be easier to have one text control in a fixed-width font (or in a font where all digits are the same width and a space character of that width is also available) so I could control the layout manually (with newlines). The downside of that is that I'll then need to perform hit-testing on the control to see which item should be toggled.
Both these approaches have the downside that they don't adjust well when switching to landscape mode or to other resolution changes.
So my question is, how do I handle this in a way that can take full advantage of auto layout (i.e., minimal work for the developer). Ideally what I'm looking for is something like the Java Swing FlowLayout container, to which I can just insert those text controls in order, and it will fill it out as if it's a book (populate top line, left to right, then next line and so on).
If you have a matrix like layout and you want it to flow like the FlowLayout from Swing, you can use a UICollectionView.
I think that matches best what you are trying to achieve.
Im trying to implement a nice accordion effect on my app. My goal is to "open up" a cell when the user tap on it, diplaying additionnal content (such as text) as the cell height increase.
The perfect sample code for that is the TableView Animations & Gestures sample code provided by Apple. However I am experiencing a strange behavior, that ruins the whole effect.
It appears that depending on the order in which the tableview will display its cells (top --> Bottom or Bottom-->up) the cells textviews will overlap each other or not.
As its a bit difficult to explain with word so here is it with images.
Those screenshots were taken from the TVAnimationsGestures Sample Code, without any changes made to it. It comes from the first version of the sample code without storyboard:
Now the version with the storyboard, first behaved well, but after a while, and without me touching the code it started drawing this :
And I have the exact problem on my custom with my custom cells.
It took me a while to understand what I think the problem comes from. If cells are drawn from the Top cell to the bottom, there is no such problem. However if cells are drawn bottom to the top, they will stack in reverse and therefore overlap each other. I don't think it is possible to control this behavior.
What gave me the hint, is that when I scroll down, forcing the top cell to redraw, they actually redraw nicely, and the screen looks like this:
A Mixture of overlapping cells and "good" cells.
Again, this all comes directly from Apple sample code, without any changes.
Does anyone knows whats going on?
Thanks a lot for your help.
You can increase the cell height by pinching on the cell.
I'm using the workaround described here to vertically align text in a multiline UILabel to the top. Unfortunately that solves only half of the problem because I also want to collapse the remaining space and move other labels below the label to move up and consume the space.
Is there anyone way to achieve this without having to resort to calculating the Frame's of the other views yourself and thus taking over the layouting?
Here's a screenshot of my problem. To make it easier I've colored the background of the relevant labels. The Blue label is supposed to move up and consume the unused space of the red label. The Red Label is configured for 3 Lines and Font-Autoshrink is off.
You must re-calculate your label frames and then rearrange your labels in the view.
Or you can try to use CoreText to make more complex layouts without the need to cope with these frames rearrangement. On GitHub there is a nice work BSD-licensed, called NSAttributedString+HTML: https://github.com/Cocoanetics/NSAttributedString-Additions-for-HTML which tries to port in the iOS the same methods already available in OSX. It is based on CoreText and with this class you can write your page as html and render it on screen without using UIWebView (and without its limitations). Based on your screenshot, it would be easy to achieve the wished result with simple html.
I've had this issue come up a couple times doing iPhone development now and have yet to find a good solution for it. What I'm looking for is this:
I want to show some text along with an icon and I want the overall display to be centered within its parent view. Specifically, in my current case, I'm looking to display a box that says "Reconnecting..." with a UIActivityIndicatorView to the left of the text.
Previously, I've just relied on the fact that I know exactly the dimensions of the text and activity indicator, so I can position things absolutely to appear centered. What I'm looking for is something more automatic.
Any ideas?
One of the UIKit additions to NSString will return the pixel size of the text if you give it the font that you're using on the UILabel. So then presumably the total size of the two things together in the layout you describe is:
the difference between label.frame.origin.x and activityIndicatorView.frame.origin.x; plus
the width of the text.
You can then either shuffle both the views appropriately or give them a common parent that's still a subview of the whole thing and shift that.
An activity indicator can be shown in a view just calling a single method.Please click here get SHKActivityIndicator class