How to remove left padding from UIButton?
I have been created button with this code:
let button = UIButton(...)
button.setImage(UIImage(named: "plus")?.withRenderingMode(.alwaysTemplate), for: .normal)
button.setTitle("Text", for: .normal)
button.layer.cornerRadius = 8
button.layer.backgroundColor = UIColor.red.cgColor
You need to adjust the imageEdgeInsets and titleEdgeInsets with some negative left value. Then it will shift to left. I tested it's working. 100 is temp value.
button.imageEdgeInsets = UIEdgeInsets(top: 0.0, left: -100.0, bottom: 0.0, right: 0.0)
button.titleEdgeInsets = UIEdgeInsets(top: 0.0, left: -100.0, bottom: 0.0, right: 0.0)
Let me know if it is not working.
This line could fix your issue
button.titleEdgeInsets.left = 0 // add left padding.
Or maybe you could use negative value in this case
There is also this way :
button.titleEdgeInsets = UIEdgeInsets(top: 0.0, left: 0.0, bottom: 0.0, right: 0.0)
Just try different values !
Related
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
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
I want create button with image like this
and this is my code but it't true with other model iPhone:
extension ButtonWithImage {
func alignVertical(spacing: CGFloat = 6.0) {
guard let imageSize = self.imageView?.image?.size else { return }
guard let titleSize = titleLabel?.intrinsicContentSize else{return}
// Title Edge Insets
let titleEdgeInsets = UIEdgeInsets(top: 0.0 ,
left: -imageSize.width,
bottom: -(self.frame.size.height + 2*spacing),
right: 0.0)
self.titleEdgeInsets = titleEdgeInsets
self.imageEdgeInsets = UIEdgeInsets(top: 0.0,
left: 0.0,
bottom: 0.0,
right: -titleSize.width)
self.contentEdgeInsets = UIEdgeInsets(top: spacing,//edgeOffset,
left: 0.0,
bottom: spacing,//edgeOffset,
right: 0.0)
}
}
what is value of bottom in titleEdgeInsets to working the same with any model iPhone?
please use below extension for simple use
extension UIButton {
func alignCenter(withPadding: CGFloat = 6.0) {
guard
let imageViewSize = self.imageView?.frame.size,
let titleLabelSize = self.titleLabel?.frame.size else {
return
}
let totalHeight = imageViewSize.height + titleLabelSize.height + padding
self.imageEdgeInsets = UIEdgeInsets(
top: -(totalHeight - imageViewSize.height),
left: 0.0,
bottom: 0.0,
right: -titleLabelSize.width
)
self.titleEdgeInsets = UIEdgeInsets(
top: 0.0,
left: -imageViewSize.width,
bottom: -(totalHeight - titleLabelSize.height),
right: 0.0
)
self.contentEdgeInsets = UIEdgeInsets(
top: 0.0,
left: 0.0,
bottom: titleLabelSize.height,
right: 0.0
)
}
}
When a UITableView governed by a UITableViewController has cells with text fields in them, if the user taps in a text field, the table view scrolls to keep that text field above the keyboard.
How does it do that? I tried to figure it out by implementing this method with a bunch of logging:
override func scrollViewDidChangeAdjustedContentInset(_ scrollView: UIScrollView) {
print("did adjust")
print("contentInset", self.tableView.contentInset)
print("indicatorInsets", self.tableView.scrollIndicatorInsets)
print("adjustedContentInsets", self.tableView.adjustedContentInset)
print("safe area", self.tableView.safeAreaInsets)
print("additional safe area", self.additionalSafeAreaInsets)
}
Here's the output from before and after the tap-and-scroll:
did adjust
contentInset UIEdgeInsets(top: 0.0, left: 0.0, bottom: 0.0, right: 0.0)
indicatorInsets UIEdgeInsets(top: 0.0, left: 0.0, bottom: 0.0, right: 0.0)
adjustedContentInsets UIEdgeInsets(top: 64.0, left: 0.0, bottom: 0.0, right: 0.0)
safe area UIEdgeInsets(top: 64.0, left: 0.0, bottom: 0.0, right: 0.0)
additional safe area UIEdgeInsets(top: 0.0, left: 0.0, bottom: 0.0, right: 0.0)
did adjust
contentInset UIEdgeInsets(top: 0.0, left: 0.0, bottom: 0.0, right: 0.0)
indicatorInsets UIEdgeInsets(top: 0.0, left: 0.0, bottom: 0.0, right: 0.0)
adjustedContentInsets UIEdgeInsets(top: 64.0, left: 0.0, bottom: 253.0, right: 0.0)
safe area UIEdgeInsets(top: 64.0, left: 0.0, bottom: 0.0, right: 0.0)
additional safe area UIEdgeInsets(top: 0.0, left: 0.0, bottom: 0.0, right: 0.0)
After the scroll, the bottom of the adjustedContentInsets has changed. But how? It's not a settable property. It's supposed to respond to the safe area, but the safe area has not changed. The contentInset has not changed. How is this accomplished? How would I do the same thing with my own scroll views? (My solution currently is to change the contentInset, but clearly the table view controller doesn't have to do that.)
I have an image and a title for a UIButton. I'm trying to get my image (15px in width) to be aligned left and my title to be aligned 5px left of that:
The button is 215 in width, but I had to set my titleEdgeInset right to -44. This doesn't make sense. What are the maths to get -44?
It's because the image and title of a button are centered together by default.
For example, here's a 300px wide button with an image and text, no insets:
Looks like I need the button contents shifted left by 60 pts, so -60 left inset and 60 right inset.
button.imageEdgeInsets = UIEdgeInsetsMake(0.0, -60.0, 0.0, 60.0)
button.titleEdgeInsets = UIEdgeInsetsMake(0.0, -60.0, 0.0, 60.0)
Edit:
These insets would also work for the above example.
button.imageEdgeInsets = UIEdgeInsetsMake(0.0, -120.0, 0.0, 0.0)
button.titleEdgeInsets = UIEdgeInsetsMake(0.0, -120.0, 0.0, 0.0)
The button is centering its contents, so if you say "there are 2 more pts on the left" the button will move its contents over by 1 pt to keep them centered.
The rule is
-leftInset+rightInset=leftMargin*2
UIButton is centered by default like the other answers stated, but you can change that to achieve a more intuitive inset behaviour.
Select left alignment in the attributes inspector.
Then set insets like this:
button.imageEdgeInsets = UIEdgeInsets.zero
button.titleEdgeInsets = UIEdgeInsets(top: 0, left: 5, bottom: 0, right: 0)
It's easy to do:
In swift:
let button: UIButton = UIButton(frame: CGRect(x: 44, y: 64, width: 100, height: 40))
button.backgroundColor = UIColor.clear
button.setImage(UIImage(named: "img"), for: .normal)
button.setTitle("hello", for: .normal)
button.layer.borderWidth = 1
button.layer.borderColor = UIColor.blue.cgColor
button.layer.cornerRadius = 4
// set edge inset
button.imageEdgeInsets = UIEdgeInsets(top: 0, left: -40, bottom: 0, right: 0)
button.titleEdgeInsets = UIEdgeInsets(top: 0, left: -20, bottom: 0, right: 0)
view.addSubview(button)
In objective-c:
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(44, 64, 100, 40);
button.backgroundColor = [UIColor clearColor];
[button setImage:[UIImage imageNamed:#"img"] forState:UIControlStateNormal];
[button setTitle:#"hello" forState:UIControlStateNormal];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
button.layer.cornerRadius = 4;
button.layer.borderColor = [UIColor blueColor].CGColor;
button.layer.borderWidth = 1;
// set edge inset
button.imageEdgeInsets = UIEdgeInsetsMake(0, -40, 0, 0);
button.titleEdgeInsets = UIEdgeInsetsMake(0, -20, 0, 0);
[self.view addSubview:button];
[![The result][1]][1]
[1]: https://i.stack.imgur.com/FyzqB.png