UIButton's image and title edge insets programmatically in Swift - ios

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?

Related

UIcollectionViewCell disappear when UICollectionView.contentInset set

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

How to set the margin of backBarButtonItem?

How to set the margin of backBarButtonItem ?
Using these methods has no effect:
backItem.imageInsets = UIEdgeInsets(top: 0, left: 100, bottom: 0, right: 0)
backItem.width = 100
self.viewControllers.last?.navigationItem.backBarButtonItem = backItem

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

how to give padding to a label text in collectionviewcell in swift?

I have UICollectionViewCell and inside cell their is one label without set width constraints that text automatically set ,but I need left and right spacing from the text in the label in swift
You should have custom UILabel class and in that class override drawTextInRect :
override func drawTextInRect(rect: CGRect) {
var insets: UIEdgeInsets = UIEdgeInsets(top: 0.0, left: 10.0, bottom: 0.0, right: 10.0)
super.drawTextInRect(UIEdgeInsetsInsetRect(rect, insets))
}
If your label has more than one line it's better to use UITextView and change textContainerInset property:
let textView = UITextView()
textView.textContainerInset = UIEdgeInsets(top: 0.0, left: 10.0, bottom: 0.0, right: 10.0)

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)

Resources