UIcollectionViewCell disappear when UICollectionView.contentInset set - ios

My problem is..
when I set contentInset like below
membershipCollectionView.contentInset = UIEdgeInsets(top: 1, left: 0, bottom: 1, right: 0)
UICollectionViewCell disappear. If I change the parameter value of top, bottom to 0 it appears. Is there anyone who explain this disappearing situation?
On contentInset = UIEdgeInsets(top: 1, left: 0, bottom: 1, right: 0)
On contentInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
Here is my UICollectionView and UICollectionViewCell
UICollectionView
height = 50
also I set estimatedItemSize to automaticSize
#IBOutlet weak var collectionLayout: UICollectionViewFlowLayout! {
didSet {
collectionLayout.estimatedItemSize = UICollectionViewFlowLayout.automaticSize
}
}
UICollectionViewCell
height = 40
this is label inside UICollectionViewCell. Height is 40

Related

Tableview cell padding and spacing

I have this table with cells which have an image and a text
First thing that I want is to add a padding to cells so that the image with the circle and the scissors doesn't hit the border of the cell. I already tried things like cell.layoutMargins = UIEdgeInsets(top: 50, left: 50, bottom: 50, right: 50) or cell.bounds = CGRect(x: 0, y: 0, width: 100, height: 100), cell.separatorInset = UIEdgeInsets(top: 100, left: 100, bottom: 100, right: 100) but nothing has changed.
Second thing that I would like is to add a spacing between the cell, I tried all the solutions that I found but it doesn't seem to work
CODE (some of the solutions that I tried are written below all toghether, but I also tried them one by time)
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "difficultyCell", for: indexPath)
cell.textLabel?.text = myDict[indexPath.row].key
cell.textLabel?.font = .boldSystemFont(ofSize: 20)
let imageCell = UIImage(named: "scissors.png")
cell.imageView?.image = imageCell
cell.backgroundColor = Esercizio.hexStringToUIColor(hex: "#4cf584").withAlphaComponent(0.85)
cell.layer.borderColor = UIColor.orange.cgColor
cell.layer.borderWidth = 1
cell.layer.cornerRadius = 8
cell.layoutMargins = UIEdgeInsets(top: 50, left: 50, bottom: 50, right: 50)
cell.bounds = CGRect(x: 0, y: 0, width: 100, height: 100)
cell.separatorInset = UIEdgeInsets(top: 100, left: 100, bottom: 100, right: 100)
return cell
}
to solve it easily , if you are using custom UITableViewCell file, in your cell class override the layoutsubView method of the class and there you can add the UIEdgeInset to the frame of the contentView, and also dont forget the added padding amount into total RowHeight estimation in your tableview: this code will work for you for sure,
override func layoutSubviews() {
super.layoutSubviews()
contentView.frame = contentView.frame.inset(by: UIEdgeInsets(top: 0, left: 0, bottom: 10, right: 0))
}
this way you can create padding to whatever direction you want to add the padding.
The best solution here is to create your own cell, add your own image, labels, etc. inside of contentView and tweak them how you want using auto layout constraints.
see: https://www.raywenderlich.com/8549-self-sizing-table-view-cells

The space between title and text on UIButton is not correct when adjusting titleEdgeInsets and imageEdgeInsets

I want to adjust the space between text and image on UIButton,
let space = 10
button.contentHorizontalAlignment = .left
button.titleEdgeInsets = UIEdgeInsets(top: 0, left: space, bottom: 0, right: 0)
It looks well, the space in the picture is absolutely 10.
And now, I want them center,
let space = 10
button.contentHorizontalAlignment = .center
button.titleEdgeInsets = UIEdgeInsets(top: 0, left: space, bottom: 0, right: 0)
It looks much smaller, and the space is only 5. I find it from Reveal.
Why the space is reduced by half?
I searched, and this tells me how to make title and image center as a single entity. And it adjust their space like this:
CGFloat spacing = 10; // the amount of spacing to appear between image and title
tabBtn.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, spacing);
tabBtn.titleEdgeInsets = UIEdgeInsetsMake(0, spacing, 0, 0);
Yes, it truly works well, but why? From the letter, the space should be 20, doesn't it?
Here is an example, any helps?
Thanks in advance.
You Didn't set imageEdgeInsets like this :
rightButton.imageEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 10)
So total Code is here :
leftButton.contentHorizontalAlignment = .left
leftButton.imageView?.backgroundColor = .red
leftButton.titleLabel?.backgroundColor = .gray
leftButton.titleEdgeInsets = UIEdgeInsets(top: 0, left: 10, bottom: 0, right: 0)
rightButton.contentHorizontalAlignment = .center
rightButton.imageView?.backgroundColor = .red
rightButton.titleLabel?.backgroundColor = .gray
rightButton.imageEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 10)
rightButton.titleEdgeInsets = UIEdgeInsets(top: 0, left: 10, bottom: 0, right: 0)
Reason:
Actually you need to set imageEdgeInsets both time left alignment or center alignment
But when its left alignment image there is no space to set Insets at the right side.
Seet this code :
leftButton.imageEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 100)
Here is also no space .
So home you get some idea how insets work :
For your more Help :
Click here

UITabBarController icons changing height on touch

I have an issue with UITabBarController, the icons are changing size if I keep my finger on them and drag up or down, they are losing height by my dragging action, how can I stop that?
Here is my code:
override func viewWillAppear(_ animated: Bool) {
.................
vc_friends.tabBarItem.imageInsets = UIEdgeInsets(top: 6, left: 0, bottom: -5, right: 0)
vc_topCharts.tabBarItem.imageInsets = UIEdgeInsets(top: 6, left: 0, bottom: -5, right: 0)
vc_newsfeed.tabBarItem.imageInsets = UIEdgeInsets(top: 6, left: 0, bottom: -5, right: 0)
vc_myProfile.tabBarItem.imageInsets = UIEdgeInsets(top: 6, left: 0, bottom: -5, right: 0)
vc_pools.tabBarItem.imageInsets = UIEdgeInsets(top: 6, left: 0, bottom: -5, right: 0)
self.viewControllers = [vc_friends,vc_topCharts,vc_newsfeed,vc_pools,vc_myProfile]
self.selectedIndex = 2
}
This is how it looks like:
...and this if I click on it and keep my finger on it and move my finger up or down:
This is a known issue when your negative value is not the opposite of the positive value in the other direction. Change your insets to this:
UIEdgeInsets(top: 6, left: 0, bottom: -6, right: 0)

UITableView Section Header Stop Position

Is it possible to give the section header in ui tableview an offset to stop programmatically?
So, that the section header stop 100 px from top?
I think this should work:
override func scrollViewDidScroll(_ scrollView: UIScrollView)
{
super.scrollViewDidScroll(scrollView)
let inset: CGFloat = 73
if scrollView.contentOffset.y < inset && scrollView.contentOffset.y > 0 {
scrollView.contentInset = UIEdgeInsets(top: scrollView.contentOffset.y, left: 0, bottom: 0, right: 0)
} else {
if scrollView.contentOffset.y < 0 {
scrollView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
} else {
scrollView.contentInset = UIEdgeInsets(top: inset, left: 0, bottom: 0, right: 0)
}
}
}
You can try this one
CGFloat newViewHeight = 40;
UIView *newView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.bounds.size.width, newViewHeight)];
self.tableView.tableHeaderView = newView;
self.tableView.contentInset = UIEdgeInsetsMake(-newViewHeight, 0, 0, 0);
Section headers will now scroll just like any regular cell.
You can use property of UIScrollView: contentInset. And style for a table view UITableViewStyleGrouped (.group in Swift).
This is my example of such UI:
If you want to avoid header moving, set Bounce Vertical property to NO.

UIButton's image and title edge insets programmatically in Swift

I am setting two UIButtons programmatically which look like this:
I am trying to set the image and text insets to be the same in buttons, but somehow left button's image is located differently than the right one.
I have this extension to UIButton class:
func setTextAndImageForButton(spacing: CGFloat) {
let insetAmount = spacing / 2
imageEdgeInsets = UIEdgeInsets(top: 8, left: 4, bottom: 8, right: insetAmount)
titleEdgeInsets = UIEdgeInsets(top: 0, left: 4, bottom: 0, right: -insetAmount)
contentEdgeInsets = UIEdgeInsets(top: insetAmount, left: 0, bottom: insetAmount, right: insetAmount*1.5)
}
As far as I understand when I set the insets for image, text and whole content, it should be located in the same place for both instances.
Are the insets override somehow after I set them in code?

Resources