Center UIButton with image next to it - ios

I'm trying to create a UIButton where the facebook icon is a part of the centering, however i can't seem to figure out how to make it part of the centering process. So far i've just created a UIButton with the text. I could just create a image next to it, however then it will not be part of the centering.

You can easily set image and title both for your button for different control state like normal,selected or highlighted etc.
So, set your image and title to your button from storyboard or programmatically there is no need to take separate imageview for image.
You can set edge insets for title and image both from interface builder or programmatically. you can manage top or bottom edge insets for title and image of button to manage it's vertical appearance!
check screen shot below,
Update : (as asked in comment)
For example,
this is the setup for my back button with image!

Provide button title and image for normal state. Then just change the left inset for image as per image

Related

Stop UIButton resizing on background image change

I have built a storyboard with a label, 6 UIButtons in a 3 x 2 grid and another single button below the grid. I've put these into stack views and set up constraints which all seems to work.
I've set the background image of each button which is initially just a grey background (will be more of an icon later) and when a user clicks on the button I change the background image to be a green tick.
However, when it renders, the button increases in size so my 3 x 2 grid looks shoddy.
How can I stop the button resizing when I programmatically change the background (using setBackgroundImage function)?
Try giving some fixed width or height, cause in a stack view it always take what it needs. Or you could make sure buttons are equal between them. Click and drag a button to another with alt key and set it to equal width.

Xcode Button image top text below in a button not in code

I create a button set image title like below picture.
I want
image
title
image on the top title below is it possible without any code?
I cant just use insets because to another size will wrong
Adjust both the title insets and the image insets.
It will need some calculations, and auto layout will probably screw it up ;)
Probably better to create a subclass of UIButton with a .xib
click the link to view my screenshot

Label over a button

I'm trying to put a label over a button, but I'm not succeeding...
Here's what I have. I'd like the text "tap to dial" to actually be over the dark grey area, not below it. How do I do that?
On the left side of the image is the view hierarchy, the red things left and right are the button add and button remove, so you get the whole picture.
EDIT: Explanation - The label I'm trying to place over the button is the hint what the button does. The button itself will hold different text contents, thus I need an additional label.
EDIT 2: I don't know why is this question marked with negative votes, why don't people explain that in the comments? Let's pose the question differently: if one wanted to put a label over an image, how would that be done?
Select both objects in IB (Interface Builder) and then create constraints so they have both have the same vertical and horizontal center. If the text is under the button then select the text view and choose "move to front" from the editor menu.
All that being said, why don't you use a titled button with a gray background rather than trying to put a label on top of your button?
A button is a label (that is, its title is). So you don't need a button and a label; just configure the label of the button. The label is the button's titleLabel and you can make it do anything a label can do — including consisting of multiple lines, one of which is smaller than the other, as shown here:
The solution I was looking for was to embed the button and the label in a view. Inside the view they can be positioned to overlap each other, while the view itself can be constrained by auto-layout.

UIButton background image horizontal scale to fill while maintaining aspect ratio

I am trying to set the backgroundImage of a UIButton to an image that could be any size. The way I want to display tall images is for the images to expand to fill the button horizontally, and only show the top part of the image. So for a button with dimensions 100x200 and an image of 80x400, I want to scale the image to 100x500 and only show the top 200 pixels of that scaled image.
I am wondering if there is some way to alter how the background image fills the uibutton (right now it would not preserve the aspect ratio of the image). I tried setting the contentMode of the button and the button's imageView (note I set the button to a Custom Type, not System) to .ScaleAspectFill but that did not do anything.
The closest I came was by ditching the background image entirely and using setImage instead. This code was able to fill the button horizontally, but it shows the middle of the image, not the top. It also had odd functionality where if the scaled image was the same height as the button, it was not showing the full image (about 5 px were cut from the bottom and top). Here is that code:
imagesButton.imageView?.contentMode = .ScaleAspectFill
imagesButton.contentHorizontalAlignment = .Fill
imagesButton.setImage(image, forState: .Normal)
I have looked at about 5 stack overflow posts regarding image resizing in UIButtons, but I can't find the right set of settings that solve my particular need.
I don't know if this solution is an option for you, but the best way to achieve what you're trying to do, and give you pretty as much flexibility as you'd like with positioning and scaling the image, it to use a UIImageView (maybe even with a UIView that wraps it, for more image processing options) with a transparent custom button on top of it.
The upside to this approach is that you'll be able to do pretty much anything you want with the image and the way you display it. The downside is that you lose some of the default UIButton features (such as automatically altering the image when touched). But you can achieve these features programmatically if you want.

Display custom overlay image when button is pushed

The Goal:
I would like to add a method to my UIButton which randomly displays the following;
An image in the middle of the overlay displaying a picture
A UIButton at the top-right (red 'x') that closes the overlay
Various UILabels with information
How would I go about creating an overlay, and how would I customize the layout of the overlay and assign values and images dynamically?
I know how to create a custom cell for a UITableView, but I need a bit of guidance on this one.
Use IB or create by program a UIImageView to contain your image on top of the overlay. Then just use the button to toggle the image view between hidden (property of UIimageview) and not. Alternatively you may position push the image view to top and put it down to the bottom of the view heap using the button.

Resources