make UIButton tap area larger - ios

I have a lot of buttons, but they are quite small, and I dont want to make them bigger, I just want to make tap area bigger. I tried
func makeInsets(button: UIButton) {
button.imageEdgeInsets = UIEdgeInsets(top: -40, left: -40, bottom: -40, right: -40)
}
but this make bigger only image. Any advices on this case?

You can do this by making the button frame bigger and then adjust it with EdgeInsets:
1 Select your button;
2 Go to this menu and at "Edge" category;
3 Select "Content" and chose "Image";
4 Adjust "top" "bottom" "left" Right" insets to adjust image inside button
For this to work Image must be added to button with "..setImage" and not "..setBackgroundImage"
If "Content" is selected it will modify both: Images and Text inside button
It can also be done programatically but it's a little bit harder for not being able to see live modifications and you must calculate.
Hope it helps.
Update:
As an update to your question update you could try:
-change -40 into 40 : this will make your button remain bigger and image inside will smaller in every side with 40 and it will answer to your question
if you want to modify whole button (text + image) you whould use btn.contentEdgeInsets instead btn.imageEdgeInsets and set values

Make the frame of the button bigger, but change the view mode in interface builder to center the image, and not fill. This keeps the image at the original size but the borders of your button extend invisibly beyond.

Related

Different title origin of UIButton with image inside in different iOS versions

I created a button in interface builder.
Set fixed width = 120 and height = 40 constraints to this button.
After I add an image to button and set image insets (top: 10, left: 0, bottom: 10, right: 100) to move the image to the left side.
UIButton insets screenshot
The problem:
In different iOS versions (in my case it’s: 10.3.1, 12.1 and 13.1) the button drawing differently.
Button’s title origin is (x = 29.5, y = 11) for iOS 13.1 and (x = 44, y = 11) for older iOS.
Simulators screenshot
How to make buttons look identical in all iOS versions?
i know what you're trying to do and just don't. it's going to crush you trying to do this as you think it should work. instead do this
make your button as you did above
add a UIImageView to the button, so the UIimageView is added as a subview to the UIButton
add the constraints on the UIImageView so that the image view is on the very left side of the button
this is how you hack around with iOS to make things work that should work but don't, and without needing to subclass the UIButton and do some crazy geometry math's with it's own inner imageview.
My research revealed a possible reason for such a different drawing of buttons.
And the reason is imageView’s old frame.
After adding image insets to move image to left / right, the image frame changes.
I don’t know how exactly Apple calculates title's origin of button.
In older iOS title’s origin.x is equal image’s old frame.width.
In iOS 13 title's origin changes as I expected.
So i decided to resize an UIImage before I put it into UIImageView.
And it works pretty well.
Thanks to everyone who gave advice.

How can I add space above and below an ImageView in a row in a TableView?

The row height in this table view is 54. The ImageView Height is 44. I'd like 5 above and below the ImageView (some spacing the image so that they aren't touching each other). Basically, centering the ImageView, vertically, in the row (or ContentView). No matter what I've tried, the image is always anchored in the top left corner of the row (since its a music app, it has to run on a phone, so I don't have an image for the result). I'm embarrassed to say that I've spent hours trying to accomplish this. I'd appreciate any help. Thanks.
If I were you, this is how I would set up my constraints. First, I would set the width constraint on the image view to be 44. [Note: I set my row height to be 54 just like yours.] Then I would set the aspect ratio constraint on the image view to be 1:1 (assuming you want a square image). Then, I would put the image view and the label to be in a horizontal stack view. If you bundle everything up in a stack view, I find that you have to worry about less constraints (the stack view will take care of the spacing between the image view and label although you can adjust accordingly if needed). Then I would add leading and trailing constraints to the stack view. And then finally, center the stack view vertically in the container.
Hope this helped. Good luck!
I was right, there was another place that was forcing the image into the top left corner. It was in the code when setting the contents of a row as shown below. I wrote that part a long time ago when I knew even less about Swift (the goal was to make sure the image gets scaled. It may not be necessary though. I'll test the code without it. Still, thank you Zaya for taking the time to help!
let albumArtwork = rowItem.representativeItem?.artwork
let tableImageSize = CGSize(width: 10, height: 10) // This doesn't matter, it gets resized below
let cellImage: UIImageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 44, height: 44)) // The albumImageView is 44 by 44

iOS Hide button with equal width constraint

So I am trying to implement some feature restrictions based on permissions in an app. The feature in question works on a button that is placed at the bottom of the VC in main.storyboard.
There is another button that I want to remain there. They are aligned horizontally, but not in a horizontal stack view. They have an equal-width constraint and together take up the whole vertical space at the bottom.
I want to hide the first one, and thus have the second one take up that entire horizontal space from left to right, but only when the access to the first button is supposed to be restricted. I've tried doing this in viewNeedsRefresh:
let equalWidthConstraint = firstButton.superview?.constraints.filter{
$0.firstItem as? UIButton == firstButton && $0.secondItem as? UIButton == secondButton
}.first
equalWidthConstraint?.isActive = false
firstButton.isHidden = true
secondButton.frame = CGRect(x: 0, y: firstButton.frame.origin.y, width: self.view.frame.width, height: firstButton.frame.height)
But all this does is hide the first button, while the second button is suddenly partially off-screen. If I try without manually disabling the constraint, I get it looking cleaner but the second button is still in its original position, likely because it still maintains the original width due to the constraint.
Personally, in my opinion, I'd do the following:
remove the equal widths constraint
set a width constraint on one of the buttons set to 0.5x the superview width, then create an outlet for the constraint
set the other button with a trailing or leading (depending on the button you chose) to zero.
When you need to collapse the button, you can now set the constraint's constant directly like so:
buttonWidth.constant = 0
That would hide the view and give you the desired effect.
It turns out they built a re-usable component for this in the past before I joined, but did not actually use it more than once. I have converted the page to use that component instead. Sorry guys.

Storyboard Constraints in iOS

I am new to iOS and i want to set constraints to 3 buttons like following image.
First i want to set Background view (Green colour) = (0, 100, device width, 150);
then 3 buttons on the Background view (buttons are subviews of Background view).
Buttons height and width is fix i.e., 100 and 100.
Space between the buttons is 50 and buttons y position is 5
please help me to set constraints, the following image illustrate the Design. Thanks in advance
A bit tricky to explain, but I'll try, so select button2 and click the pin option and add the five constaints.
The select button1, add the following constaints and repeat for button3.
Finally you can align button2 to center horizontially.
Hope that helps, also read through the link Ulli posted, it's a good one, good luck.

Adjust width of UITextField to fill toolbar in landscape

In a UIToolbar, I have added a UITextField to the middle of the bar (which gets added as a UIBarButtonItem) with another UIBarButtonItem (Action button) next to it. I added flexible space bar button items at the very left and very right. It looks great on portrait, but when I rotate to landscape it centers them and looks ok, but I need the text field to get stretched to fill the width and push the last button clear to the right - exactly like it does in Messages - on iPhone and iPad.
I thought Auto Layout would do the trick but Apple states you cannot create constraints for UIBarButtonItems. And indeed when I select any of the bar buttons or the text field it will not allow creating any constraints.
Could you please let me know how to accomplish this? Thanks!
EDIT: I've tried a few other combinations with fixed/flexible spaces. When I don't add any, the text field and share button are pushed over to the left:
If instead I do fixed spaces, it looks the exact same as above with 0 widths, or if I set a width then it obviously will push them over. That last space on the far right has no effect - it's not fixed against the far right side so it goes off the screen.
I tried a fixed space on the left and a flexible on the right, with the middle one fixed (or no middle one), and it looks like the screenshot above. I then changed the middle one to flexible and it turned out like this:
If I change the first to flexible, none in the middle (or if I add a fixed one), and flxed on the right, it is moved over to the right:
If the first is flexible, middle is flexible, and right is fixed, this is how it looks:
In all cases the width of the text field remains the same static value as it was originally set up in the storyboard. I think the problem is that when you set the width by dragging the frame, there are no blue guidelines to snap to so it will always remain that same width.
Did I miss any combination of spaces? If I cannot implement the desired behavior in the interface builder, how would one accomplish this in code?
To obtain a horizontally stretching text field, I had to manually set its frame in viewWillAppear and also when the orientation changes - I used viewWillTransitionToSize:withTransitionCoordinator::
[coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> __nonnull context) {
self.textField.frame = CGRectMake(0, 0, self.navigationController.navigationBar.frame.size.width - self.actionButton.width - 55, self.inputTextField.frame.size.height);
} completion:^(id<UIViewControllerTransitionCoordinatorContext> __nonnull context) {
}];
Previous solution (no longer works for me in latest OSes):
Add this in viewDidLoad:
self.textField.autoresizingMask = UIViewAutoresizingFlexibleWidth;

Resources