I have a UICollectionView with just 1 section and many cells in that section. I've enabled drag and drop behavior (with a long press gesture recognizer, because the UICollectionView sits in a generic UIViewController).
I'd like the drag and drop behavior to detect when the dragged cell is over the top of another cell. Google search didn't yield anything helpful, but Stackoverflow has this solution. While I can try to implement that by writing something similar in the UIViewController class to check if the dragged cell's Rect intersect with every other cell, I'm wondering if there's a more efficient way to accomplish this goal?
Off the top of my head, I think you could just grab the min and max X or Y coordinates (depending on whether the collection view is horizontal or vertical) of the dragged view, and then do a binary search through the views in the collection until you find one that intersects. This way, you wouldn't have to test all the views.
EDIT: Adding an example to clarify what I mean. Suppose we have a collection of views that are arranged left-to-right:
0 1 2 3 4 5 6 7 8 9 10
We start by getting the min and max X values of our dragged view. We'll call those "minX" and "maxX". We start by looking at the view in the middle of our list, view 5. If maxX < view 5's min X, then the view we're looking for has to be one of 0...4. If minX > view 5's min X, then we're looking for something in 6...10. Otherwise view 5 is intersecting the dragged view in the horizontal dimension.
After testing view 5, we test either view 2 or view 8, depending on whether the dragged view is to the left or right of view 5. We continue via this method until we happen upon an overlapping view, and each time we test a view we eliminate half of the views from consideration, so it will perform better than having to test every view in the collection.
Once you find one view that overlaps the dragged view, you'll want to check the adjacent views to see if they also overlap. In the case where the dragged view overlaps with several other views, you'll have to choose the one that overlaps the greatest area.
Related
I am trying to get a layout working where I have 9 squares set 3 x 3 and on all device sizes, they are square.
I have tried endless ideas to make it work but can't seem to get it to stay squares on all devices.
I attached below, a picture showing the results and current constraints on the top left corner square.
Any help would be awesome!
The best approach would be use the stackView. The advantage will be you do not have to deal with the much constraints. So select the first rows three view horizontally then click on the Embed in Stack button whose axis should be horizontal inside your storyboard. Follow the same for second and third rows. Also inside stackview you can mention the spacing you want.
So now you have three stackView for all the three rows. After that select all three stackView then click on the Embed in Stack button and whose axis should be vertical and you can mention the spacing you want.
So advantage of doing that is you do not have to worry about the constraints. Finally you only have to apply the constraint on your main stackView which hold all your child stackView
While I totally agree that UIStackView is a great option, you can also add Aspect Ratio constraints (with a Multiplier of 1) to your squares and ensure that they remain squared (as nothing about your current layout demands that your views should be squares).
If you want your 9 squares to remain in the center of the superview, I recommend adding them to an invisible intermediate view and center that within the superview.
my collection view cell is full size..i have collection view cell and it contains button..i successfully display all button with it's title in collection view cell. but i am having one issue that some button title is too large..i want to change it's widht based on text...how to set button width based on text..i have vertical collection view
my array is
var arr = ["butto1 butto1 butto1","butto1 butto1butto1butto1","butto1","butto1 butto1butto1butto1"]
I have searched so many time but still i haven't point out
...any on help me..Thank you
You haven't given much information that would help in diagnosing your problem.
The button should automatically resize to fit the text unless you have set constraints that stop it from growing.
If you have set a left and a right constraint to equal a constant then it will not be able to grow.
As far as I can tell, you have two options.
Only set 3 out of 4 constraints. For example the following image has 3 constraints set which allow the button to grow downwards.
Set the right constraint to "Greater than or equal to" instead of "equal" in the interface builder. This will allow the button to grow to a maximum size which is specified by the constraints.
Also make sure to set your button to word wrap so that text continues on the next line.
I would like to build a TableView where when the user clicks on a cell it expands to show more information. My question is how do I use Autolayout to arrange the multiple items in each cell.
Each cell will always be the same size, whether it is collapsed or not, so the sizing isn't dynamic.
The first problem I have is how to use Autolayout to arrange all the items in the cell. Before Xcode 7 I was successfully using Autolayout where I would pick a label-button-view to arrange, click Editor > Align > Trailing/Leading/Top Space, to.. etc. This is now greyed out and I don't know how to replace my old strategy.
Each cell has two rows of items. The first row shows all the time, the second only shows on collapse. Below is a picture of how the cell will look when it is collapsed:
The first row is a bit trickier because outlet1 and outlet 2 will have variable sizes. I would like 'label' to come right after label1, no matter how long or short that outlet happens to be. As arranged currently, there is a variable amount of space between the two.
What I'm looking to achieve in row 1 is basically exactly like Venmo:
Notice how 'paid' conforms to the size of the two names in the first row.
The second row has two buttons and an outlet which will always be the same size.
To sum up - how can I layout these elements in the UITableView for iphones4 thru 6S - and then how do I make this cell a collapsible cell? The construction of these cells seem to work as a system, not isolated from the whole - which is why this is a 2-part question.
is this what you want to achieve?
I'd like to use a UItableView to show a day Calendar. Each row corresponds to one hour. I need to show the hour between 2 cell of my tableview.
Like this :
(source: free.fr)
And this is my UITableViewCell :
(source: free.fr)
In the first screenshot, it works perfectly but if I scroll down then scroll up, my time label is cut like this :
(source: free.fr)
Have you any tips to figure out this problem using a tableView ?
The way you lay out your cell now is fragile, because the order of painting the cells on screen matters a lot. Try moving the content up so that your buttons are flush with the top of the cell, and the time label fits into the cell entirely. Add a thin header view to your table to make the top cell appear normal. Keeping the content of a cell entirely within its bounds should help you maintain reasonable scrolling speeds.
EDIT : You could also put a second clipped label at the top of your cell, and make its content identical to that of the label in the prior row. You would need to take special care to hide that label in the top row, but otherwise this should make your table immune to changes in the rendering order of its cells.
Make the background color of your cell clear. As you scroll up the z ordering of your cells get reversed and the lower cells overlap the higher ones causing this clipping.
This may sound a newbie question, however I'm new to iOS dev.
I've got a UITableView on my iPad app. TableView has obly three rows, is there a way to tell UITableView to view rows vertically centered, i.e. to not from the top to down.
Figure out the sum of the heights of all 3 rows, call it MyTotalHeight.
float MyTotalHeight = heightOfRow0 + heightOfRow1 + heightOfRow2;
Set your
tableView.frame = CGRectMake(start_X, start_Y, tableWidth, MyTotalHeight);
If you want the contents of each row/cell to be centered vertically within the cell, this will depend greatly on what is in the cell. You will need to calculate the height of the content and then center that content vertically within the cell by adjusting it's frame.
You may want to try the UiTableView.sectionHeaderHeight property. Play with the number until the cells are centered vertically. If your using a plain table view, I don't know how well this will work for you.
--John