Image clicked button issue in Swift - ios

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..

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

Center title on the button image

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 😊

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:

UIButton inside UIContainerView triggered in ParentView

In my containerView I placed 2 Buttons (check and cross)
I would like to access them from my ParentViewController.
The Container also got a gestureRecognizer on it.
I got the outlets going and the buttons perusable, but nothing seems to happen. Any ideas?
Got the answer myself by now:
Made two outlets of the buttons + added the Target after that
checkButton.addTarget(singleModeVC(), action: "checkButton_click:", forControlEvents: UIControlEvents.TouchUpInside)
closeButton.addTarget(singleModeVC(), action: "closeButton_click:", forControlEvents: UIControlEvents.TouchUpInside)

How to keep UIButton highlighted until a second touch?

I want to keep a button in highlighted state image until a second touch to release it to the normal state.
I've try the dispatch_async method, but it simply couldn't be back to normal state after another click.
(I'm coding in Swift so performSelector:WithObject method doesn't work either.)
I will use selected state instead of highlighted. UIButton has already the property so you don't need to create any other property.
button.setImage(image, forState: UIControlState.Normal)
button.setImage(selectedImage, forState: UIControlState.Selected)
button.addTarget(self, action: "buttonTapped:", forControlEvents: UIControlEvents.TouchUpInside)
func buttonTapped(sender:UIButton)
{
sender.selected = !sender.selected;
}
The best solution is extend the UIButton class, add the "highlited" BOOL flag. After each click just update this flag and set different image.

Resources