Center title on the button image - ios

I want to change button image and title programmatically. When design button image and title (plain) on storyboard it is centered no problem. But when ı use;
startButton.setImage(UIImage(named: "StartButton"), for: .normal)
startButton.setAttributedTitle(NSAttributedString(string: "START"), for: .normal)
to change button image and title programmatically. Button title not centered on image. It looks like it's on the left of the image.

This may helps you.
use setBackgroundImage instead of setImage
startButton.setBackgroundImage(UIImage(named: "StartButton"), for: .normal)
hope this solution will solve your problem 😊

Related

How to set an image to the UIButton in Swift

I want to set an image to the button
But every time when I add an image to the button it shows a huge image and the image doesn't fit the button size. Like this:
yourButton.clipsToBounds = true
yourButton.contentMode = .scaleAspectFill
// Use setBackgroundImage or setImage
yourButton.setBackgroundImage(UIImage(named: "yourImage"), for: .normal)
Change button style from 'Plain' to 'Default'
Click here for the image reference

Insert Icon in Navigation Bar / make BarButtonItem not clickable Swift iOS

I would like to place a bluetooth icon in my Navigation Bar, to display a connected / disconnected status.
I tried to add a BarButtonItem, set the image as my bluetooth icon, and disabled and enabled this Button. This works fine so far, and is looking ok to me, but I don't want to have this button clickable, so that it doesn't change the color on clicking on the icon.
Is this possible, or is there a way to put a UIImageView into the Navigation Bar?
Thanks!
Try disabling touch events using:
myBarButtonItem.isUserInteractionEnabled = false
navigationItem.rightBarButtonItem?.isEnabled = false
Add following code will fix your issue.
let btnBluetooth = UIButton()
btnBluetooth.setImage(#imageLiteral(resourceName: "icon_bluetooth"), for: .normal)
btnBluetooth.setImage(#imageLiteral(resourceName: "icon_bluetooth"), for: .highlighted)
btnBluetooth.tintColor = .red
let barButton = UIBarButtonItem(customView: btnBluetooth)
self.navigationItem.rightBarButtonItem = barButton
This code will add custom button where you can manage image for normal and highlight mode. For bluetooth state management, change tintColor of UIButton which you have added in UIBarButtonItem as custom view.
If you click on button, this will not change color of image.
If you don't want to add UIButton you can add UIImageView by following code.
let imgBluetooth = UIImageView(image: #imageLiteral(resourceName: "icon_bluetooth"))
imgBluetooth.tintColor = .red
let barButton = UIBarButtonItem(customView: imgBluetooth)
self.navigationItem.rightBarButtonItem = barButton
Also, make sure you have selected Render As as Template Image for your bluetooth icon which is added inside Assets.xcassets to affect tintColor. Otherwise image will be display as original. See follow:

How to add image to top layout

How to add background image on top layout the one who has back button . ive tried adding imageview on it using the drag and drop but image is hiding on the back. Anyone has an idea? Thanks
Add background image on top layout the one who has back button
Its called NavigationBar. You can set background image by this:
self.navigationController.navigationBar.setBackgroundImage(UIImage(named: "navBG.png"), for: .default)
Edit:
how about adding a label on a navigation bar?
self.title = "My Title"
Swift 4
self.navigationController?.navigationBar.setBackgroundImage(UIImage(named: "Background.jpg"), for: .default)

UISlider with different minimumTrackTintColor like shown in image preview

I need to implement change UISlider minimumTrackColor with different color according to user selection. Same like shown in image please check.Check out this example UISlider
you have to setMinimumTrackImage
slider.setMinimumTrackImage(UIImage(named: "CLR"), forState: .Normal)
CLR.png is

Image clicked button issue in Swift

How actually to make sure my button change background image when user click on it?
My code
btn1.setImage(UIImage(named:"dialpad1.png"),forState:UIControlState.Normal)
btn1.setImage(UIImage(named:"dialpad2.png"),forState:UIControlState.Highlighted)
My background image
dialpad1.png
dialpad2.png
My setting
My screen
Try using a "Custom" type in your UIButton instead of "System".
You can change it from IB:
Register your button for touchdown
myButton.addTarget(self, action: "buttonTouchDownAction:", forControlEvents: UIControlEvents.TouchDown)
Register your button for touchupinside
myButton.addTarget(self, action: "buttonTouchUpInsideAction:", forControlEvents: UIControlEvents.TouchUpInside)
func buttonTouchDownAction(sender:UIButton!)
{
//set highlighted image
}
func buttonTouchUpInsideAction(sender:UIButton!)
{
//set unhighlighted image
}
Thanks Midhun MP, your answer is most correct. I already follow what you suggested. I change State Config to Highlighted and select image in Highlight Background Image. No coding at all..

Resources