Display Image and Title Simultaneously on UIButton - Swift 3 - ios

How can I display a title and image simultaneously on a UIbutton? I tried to follow Display image and text in Button which uses swift 2 but the same process only displays the image in swift 3, with the title hidden. The image source file is 35x35 pixels. My code:
let folderButton = UIButton(frame: CGRect(x: 100 , y: 200, width: 200, height: 41))
folderButton.layer.borderColor = UIColor.black.cgColor
folderButton.layer.borderWidth = 1
folderButton.imageEdgeInsets = UIEdgeInsets(top: 0,left: 162,bottom: 3,right: 3)
folderButton.titleEdgeInsets = UIEdgeInsets(top: 0,left: 0,bottom: 0,right: 41)
folderButton.setImage(UIImage(named:"FolderIcon35"), for: UIControlState.normal)
folderButton.setTitle("Test", for: UIControlState.selected)
folderButton.setTitleColor(.black, for: .normal)
view.addSubview(folderButton)
current Output
Need a programatic solution as I am not using storyboard. Thanks!

I think you should set the title for UIControlState.normal
folderButton.setTitle("Test", for:.normal)

Try this: folderButton.setTitle("yourTitle", for: UIControlState. normal)

The problem is you are setting your title for UIButton selected mode only. SO when you select the button the title should display with your current code. For showing title in normal mode you should change the title set state to .normal.
folderButton.setTitle("Test", for: UIControlState.normal)

For UIButton in iOS, we can change the appearance of button depending upon the state, namely Normal and Selected.
Here, you are setting the title of button when button is pressed so you need to add additional line to your code by setting the state to normal.
folderButton.setTitle("Test", for: UIControlState.selected)
folderButton.setTitle("Test", for: UIControlState.normal)
For more reference on UIButton in ios, Visit: UIButton

Related

How to set width and heigh of UIButton that is added as custom view of UIBarButtonItem when using SVG images?

I am having a vector image, downloaded from phosphor-icons:
I have downloaded .svg version of an icon.
When you open this vector file in image editor, you see that it has 192x192 dimensions.
Which I guess its not relevant at all when it comes to vectors files, but...
I have this code:
let button = UIButton(frame: CGRect(x: 0, y: 0, width: 32, height: 32))
button.imageView?.contentMode = .scaleAspectFit
button.backgroundColor = .yellow
button.setImage(UIImage(named: "alien"), for: .normal)
button.setTitle(nil, for: .normal)
button.addTarget(self, action: #selector(buttonTapped), for: .touchUpInside)
let item = UIBarButtonItem(customView: button)
navigationItem.setRightBarButtonItems([item], animated: true)
Now the thing is, if I use this original svg image(192x192), a button shrinks and it looks like this:
but I don't want this button to have these dimensions, but rather those that are set with:
let button = UIButton(frame: CGRect(x: 0, y: 0, width: 32, height: 32))
Now, if I go to an image editor, and change the size of this vector file to 22x22 (it still stays svg file), it shows up like this:
which looks how I want.
The thing is, I thought that Xcode will generate required png file from this svg based on device's pixel density. So in this case, say, on newer devices that use #3x images, at compile time, cause my button is defined as 32x32 points, a 96px X 96px image will be created and used as #3x image.
Also, I thought that this is going to work like described, no matter what "dimensions" a physical .svg file is, cause it's a vector, and it should be up/down scaled as view dictates. Where I am wrong with this, and how to make this button to look the same as from second image, no matter of what are actual .svg dimensions?
EDIT:
In Attributes Inspector, for this asset, I have set Preserve Vector Data and selected Single Scale option.
Another way of to achieve this without using autolayout constraints , like you commented , is using a UIView() for customview
let view = UIView(frame: button.frame)
view.addSubview(button)
let item = UIBarButtonItem(customView: view)
self.navigationItem.setRightBarButtonItems([item], animated: true)
When adding a button as the customView for a bar button item, UIKit will automatically use auto-layout.
So, your init of UIButton(frame: CGRect(x: 0, y: 0, width: 32, height: 32)) doesn't have any effect.
I grabbed that "alien.svg" from the site you linked to and used it un-edited, with these settings:
Then, using this code (in viewDidLoad()):
let button = UIButton()
button.backgroundColor = .yellow
button.setImage(UIImage(named: "alien"), for: .normal)
button.setTitle(nil, for: .normal)
// adding the button as a custom bar button item view
// automatically uses auto-layout
button.widthAnchor.constraint(equalToConstant: 32).isActive = true
button.heightAnchor.constraint(equalTo: button.widthAnchor).isActive = true
button.addTarget(self, action: #selector(buttonTapped), for: .touchUpInside)
let item = UIBarButtonItem(customView: button)
// you can use either
//navigationItem.rightBarButtonItem = item
// or
navigationItem.setRightBarButtonItems([item], animated: true)
We get this result:

Create UIBarButtonItem with fixed size and with custom icon

I've created navigation bar like picture below
The "drop" button is a custom system UIBarButtonItem. It's contained in Right Bar Button Items and its area was expanded.
I want its size fit the image I set, like Compose button on the right.
public func setCustomButton() {
let btnSearch: UIButton = UIButton(type: .custom)
btnSearch.frame = CGRect(x: 0, y: 0, width: 25.0, height: 25.0)
btnSearch.setImage("Your image", for: .normal)
btnSearch.addTarget(self, action: #selector(yourActionName(_:)), for: .touchUpInside)
et btnBarButtonSearch: UIBarButtonItem = UIBarButtonItem(customView: btnSearch)
navigationItem.rightBarButtonItems = [btnBarButtonSearch]
}
hope, it will help you.

Custom bar button item not sizing properly

I have a custom app that includes a custom bar button item, with Swift 3 it was sized appropriately but after updating to Swift 4 it is no longer sizing to the constraints provided. This is my code:
let infoButton = UIButton.init(type: .custom)
infoButton.setImage(UIImage(named: "info button white.png"), for: UIControlState.normal)
infoButton.addTarget(self, action: #selector(StartViewController.infoButtonPressed), for: UIControlEvents.touchUpInside)
infoButton.frame = CGRect(x: 0, y: 0, width: 25, height: 25)
let barButton = UIBarButtonItem(customView: infoButton)
self.navigationItem.rightBarButtonItem = barButton
I tried changing the CGRect numbers to see if there would be any change, and there is not, it is sizing to the limits of the navigation bar, and frankly now looks quite ugly.
Any ideas about what changed in Swift 4? I am running Xcode 9.0(9A235)
Try setting constraints like this:
infoButton.widthAnchor.constraint(equalToConstant: 25).isActive = true
infoButton.heightAnchor.constraint(equalToConstant: 25).isActive = true
Or use images of the correct size.

Swift UIButton With Icon and Text (not using edgeinsets)

I'm trying to get a button with an icon to the left of some text:
Having trouble with my understanding of UIButton. Adding a title to the button works fine:
import UIKit
let button = UIButton(frame: CGRect(x: 0, y: 0, width: 300, height: 25))
button.backgroundColor = .green
button.contentHorizontalAlignment = .left
button.contentVerticalAlignment = .fill
button.titleLabel!.backgroundColor = .blue
button.setTitle("639", for: .normal)
button.setTitleColor(.white, for: .normal)
but then when you add an image
button.setImage(UIImage(named: "test"), for: .normal)
button.imageView!.backgroundColor = .clear
button.imageView!.contentMode = .scaleAspectFit
it keeps it's original size (300px) and pushes the title out the right side out of the buttons bounds. (can't see it in screenshot as its only showing button frame)
What am I doing wrong? Why does the image retain it's original width even though its been scaled down? What does contentHorizontalAlignment even do if it allows items to be pushed out of frame?
I'm aware that you can set insets but this is being used in a re-useable view so I can't hardcode values.
Thanks for the help
I just made a button just like what you said.
By the way why don't you use inset.Apple has given us the opportunity to use this.Then why shouldn't we use this?
Note: You have to make trial and error to position the image and text correctly.
Autolayout:
And inset:
If it's not accurate i said you have to make trial and error. happy coding. :)

Adding Label underneath UIBarButtonItem like in UITabBarItem [duplicate]

This question already has answers here:
Set image and title for bar button item?
(3 answers)
Closed 5 years ago.
Basically I want to achieve this using UIBarButton:
I tried using setTitle but I end up having something like this:
Create your custom navigation bar - add UIView to top of your viewcontroller, and add UIButton in left corner of it. And label underneath button.
Can be done by using custom view with UIBarButtonItem. Something like this.
Swift
let image = UIImage.init(named: "image_name")
let button = UIButton.init(type: .custom)
button.frame = CGRect.init(x: 0, y: 0, width: 100, height: 40)
button.setBackgroundImage(image, for: .normal)
button.setTitle("title", for: .normal)
let barButtonItem = UIBarButtonItem.init(customView: button)

Resources