How to programmatically change the UICollectionView Cell location - ios

How to change spacing between the navigation bar and the first collectionViewCell?
I want to be aligned in the middle. In this case, the only way to calculate the screen size?

If you want to add space at the top of your collection view you can use contentInset. There are probably other ways to meet your requirement but this is the easiest way I can think of.
collectionView.contentInset = UIEdgeInsets(top: 100, left: 0, bottom: 0, right: 0)
I don't understand what you are trying to do. Can you explain more?

Related

Add space between tableviewcell and tableview

What's the proper way to set left an right margins for a tableviewcell within a tableview?
Here's an image of what Im going for:
And here's what I actually have:
I can get the spacing on the left, but not on the right. I've set the contentInsent to this:
self.tableView.contentInset = UIEdgeInsets(top: 0, left: 16, bottom: 0, right: 16)
but it's not working either. Instead it's allowing a horizontal scroll on the tableview.
If I wanted to achieve the interface you're showing, this would have nothing to do with the table view insets. I would draw the cells to have that appearance. This would be a matter of the cell's background view. Each cell would fill the space, as intended, but the cell would draw an inner rounded rect within itself.

Last cell on UITableView cut off. Solved adding one point as contentInset: iOS bug?

I have a UITableView where the last cell is cut off behind the UITabBarController. I set the bottom constraint of the tableView to the top of my bottomLayoutGuide. I also tried this approach without success.
The only solution that worked was adding insets to the content (one point was enough):
tableView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: 1, right: 0)
Therefore I wonder if that's a normal behaviour or a scroll view bug.

TableView ReparatorInset Width - Swift 4 Without Storyboard

I'm trying to design an app with the width of the separatorInset being short. I can achieve that but the label in the cell gets short also.
tableView.separatorInset = UIEdgeInsets(top: 0, left: 205, bottom: 0, right: 10)
Is it possble to target the actual separatorInset only and not to width of the row? The label should not be affected. I only need to shorten the separator.
Result expected:
Your best bet is to use custom cells and to have a UIView with 1px height as separator.

Floating UIButton over UICollectionView

I have a UIButton, which is fixed in position, over a UICollectionView, not part of the actual UICollectionView view hierarchy.
Is there a way I am able to, on vertical scroll of the UICollectionView, to dynamically adapt the scroll length where the last row of the UICollectionView always appears above the floating button?
Haven't found anything to reference and am admittedly lost on implementation. Hope for any thoughts..
Below is the desired functionality:
You can make it so that your UICollectionView scrolls above your button by setting a contentInset to account for the height of your button (plus the buffer above and below the button. That could look something like this:
collectionView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: button.bounds.size.height + buffer, right: 0)
"buffer" is a placeholder for whatever you want to add to account for the space above and below the button.
If I understood your problem, you could do something like that:
yourButton.zPosition = 100
And your button should appear above your collection view.

How do I best create a button with a left aligned icon and the text centered in the remaining area

I need to have a button that looks like this:
Right now I went for a view with two buttons inside it. But the problem with that is that I can either hook up the two individual buttons to the same outlet, or add a UITapGestureRecognizer to the container view.
I went with the second option because I thought it would be cleaner, but that only works when I disable the buttons (and therefore disable the press-down animation).
Am I on the right track, and should I just find a way to trigger the button's animation when the UITapGestureRecognizer calls my method? Or is there a better way to do this?
You want to use a single UIButton and adjust the imageEdgeInsets & titleEdgeInsets.
For a Swift example:
extension UIButton {
func centerTextAndImage(spacing: CGFloat) {
let insetAmount = spacing / 2
imageEdgeInsets = UIEdgeInsets(top: 0, left: -insetAmount, bottom: 0, right: insetAmount)
titleEdgeInsets = UIEdgeInsets(top: 0, left: insetAmount, bottom: 0, right: -insetAmount)
contentEdgeInsets = UIEdgeInsets(top: 0, left: insetAmount, bottom: 0, right: insetAmount)
}
}
The size inspector tab for UIButton has an Insets section at the top. Play around with the insets values of title insets and image insets to achieve the desired result.
And you don't need to use multiple buttons in a view or tap gesture to achieve this. Just 1 single button would suffice.

Resources