Left aligning image on centered UIButton - ios

What is a good way to have the image edge insets of a UIButton be left aligned while having the title completely centered? Such as the following:
The image would have a constant left inset, but the title would always be centered in the entire button. Most solutions that I have found date back 4 or 5 years, and they are based on manually calculating the necessary inset of the image while keeping a center alignment for the title.
Are there really no easy ways of doing this yet?

You can set asset left aligned by using an image for your button and set horizontal alignment to left. For the title, you could add an additional UILabel and constraint it in a horizontal center-aligned manner.

you can set edge to change position of text and image of button
set Left Top Bottom and right according your requirement.

Related

Add padding to sides of text in a button

Using Xcode's storyboard, how can I add padding to the left and right of a UIButton's text? I can't for the life of me figure out how to do this.
Here is my button:
I want the button to "hug" the text and then give padding of 32px on the left and right sides of the button.
But when I try to add a Title Inset, it just squeezes the text to a point where it's almost not showing:
So how do I adjust the button's padding properly?
Ensure following things are done for the Button in storyboard.
Update Content insets value for Left and Right as 32px in Size
Inspector in storyboard.
Also, ensure Content Compression Resistance priority -
Horizontal value is high (751) for safer side.
Remove if you have any width constraint for Button, if possible.
Do not update Title insets. It will not work as you expected.
I have tested this. It's working for me.

How to implement constraints for 2 labels between 2 images for xib file

Edit: PLEASE LEAVE A COMMENT IF YOU'RE GOING TO DISLIKE SO I CAN IMPROVE MY QUESTION
I'm trying to recreate a custom table view cell in my xib file as shown below. The company's square image is on the left. The company's name and company's booth (2 UI Labels) are to the right of the company's image. The star button is to the right of the text and is a square image. I guesstimated that the company's image and favorites button should be about 8px from the top and edge.
I tried to create 4 constraints for the top, bottom, left, and right of every element (image, 2 UI labels, and button). I also added 1:1 aspect ratio constraint to the image and button to make sure the image would be square. Then I aligned the left edge of the 2 UI labels. I vertically centered the image and the button. However, it came out with no star button and the location and title switched. How do I create this design using constraints?
Their is no difficulty with that.
First if we talk about your left UIImageView, Set following constraints,
Leading constraint
Fixed Height
Fixed Width
Centre Vertically
After that the UIImageView on left, set following constraints,
Trailing space from superview
Fixed Height
Fixed Width
Centre Vertically
Now for both Labels, put them in a UIView and give that UIView following constraints,
Leading space from left image view.
trailing space from right image view.
top space from superview
bottom space from superview
Now for upper UILabel, Set following constraints,
Leading space
Trailing space
top space
Now for lower UILabel, Set following constraints,
Leading space
Trailing space
top space from upper UILabel
bottom space
After all this, i think that this will work for you.
You can use the constraints in the image below. It will work for all screen size and for any height of row.

How can I align and resize these two elements with Storyboard Auto Layout?

I am designing a very simple "Today" widget with just two elements: a text label (shown here with a red background) and an info button. I want the info button to always appear right after the label.
So:
the info button should always stick to the right margin
the label should always stick to the left margin
the label should resize itself so it is the full width of the available space, right up to the edge of the button
How can I do this?! Auto Layout is sooooo confusing and I've been playing with it for an hour with no luck. Can't figure out where to start. I think I need at least three constraints, one for each of the needs above...
Your info button will have a fixed width and height. Set its right margin to the parent right. Set your red label's left margin to parent left and right margin to info button's left.
Now since your red label doesn't have a width restriction, it will take all of the width between left margin and right margin.
This is how it looks.
You should create top, bottom, width and trailing (to the container view) constraint info button.
For the label, they should be leading, top, bottom (to the container view) and horizontal spacing to the info button.
Hope that will help.

UILabel implemente autoheight with center placement and a button to the right using AutoLayout

I want to have a UILabel with a button to it's right centered horizontally like of the picture.
I've implemented an "Align by Horizontal center" for the UILabel and standard space between label and button and it usually works just fine.
The problem appears when UILabel's text is very long and it goes out of bounds and the button is not even visible.
I've tried to set lines count for the UILabel to 0 and set questionLabel.preferredMaxLayoutWidth = CGRectGetWidth(self.questionLabel.frame)what makes the label to grow vertically. But it's width is very small (it's the base width from ui designer) so it looks like a column of text.
So the question is how to make UILabel to:
to fill all available horizontal space without hiding the button
autoscale vertically if there is no enough horizontal space to fit text into one horizontal line
be centered horizontally
It's missing the constraint from the trailing of the UIImageView to the trailing of your container view or margin. I used a Greater Than or Equal constraint so if the label has a short length, the image will stay close to the label - using the horizontal space constraint from the label to the image.
With that, your label - that is centered relative to your view, will be centered and will respect your trailing constraint of the UIImageView to the view using all the horizontal space that is possible to use as follow:
In the following image it's possible to see that the label reaches the maximum width it can, according to the constraints:
you could remove the width constraint and add a horizontal space constraint from the right side of your label to your image and one from the left side of your label to the left border of your view (set the constant to the minY Value of your Image to get it centred)

Align button with respect to background image in XCode

I have a background image and I need to put buttons in it. I have tried auto layout as well by fixing it's height and width (The button text was not part of the background, has been added manually), but it is not working for all screen sizes. I want two buttons equidistant from line that can be seen(like in iPhone 4 inch screenshot, line is part of the background). How can I go about doing that? Are there any other tools which might be helpful? I am using swift
Best way to do this is to add a hidden view which has a background color of clear. Add a constraint for this which centers it horizontally and a constraint which positions its top where you want it.
Make the width of this transparent view the space you wish between buttons or leave it as 1 wide if you wish and use offsets in the constraints described below instead. Make the height 1 so it takes up no space.
For the buttons align the top of the buttons with the bottom of the transparent view. This places the buttons vertically.
Finally pin the trailing edge of button 1 to the leading edge of the transparent view and pin the leading edge of button 2 to the trailing edge of the transparent view. Set the offset for the pins to be the distance you want the buttons from center if you gave your transparent view no width.
On all screen sizes, the buttons will now be relative to the center.
If you want to have them equally space out, you can add transparent views to the left and right of the buttons and pin them together so they act like spacers. So superview->spacer->button->spacer->button->spacer->superview.
Using the spacer approach, select the 3 transparent views and add a constraint for equal width. You buttons will now be spread apart equally.

Resources