How to keep UIButton highlighted until a second touch? - ios

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.

Related

Make selected button titlelabel to be uppercase

I have button with title "Button". After some actions this button gets "selected" status. While it is selected it have different text color that is easy to specify in interface builder.
The problem is that I also want set it's title to be uppercase. Which I don't know how.
You can set Title for Selected State of UIButton from Attribute Inspector.
Please find below image :
You can also set it programatically.
Objective-C
[btn setTitle:btn.titleLabel.text.uppercaseString forState:UIControlStateSelected];
Swift
btn.setTitle(btn.titleLabel!.text.uppercaseString, forState: .Selected)
There is a built in function for that
Button.capitalizedString
So you can use it in this way in swift
Let me assume that abtn is your IBOutlet of UIButton and Button is titlelabel of UIButton.
abtn.setTitle(abtn.titleLabel!.text.uppercaseString, forState: .Selected)
as said by kirsteins
To capitalize only the first letter you can use:
nameOfString.replaceRange(nameOfString.startIndex...nameOfString.startIndex, with: String(nameOfString[nameOfString.startIndex]).capitalizedString)
If you are setting it from code use
var myString = "string"
myString = myString.uppercaseString
button.setTitle(myString, forState: .Selected)

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)

Remove UISlider Knob or change image swift

I've added a UISlider to the view. I was wondering whether it is possible to remove the knob or just change the image in swift? How can i do this?
The slider knob is called a thumb image, and you can change it with the setThumbImage:forState: method
mySlider.setThumbImage(nil, forState: UIControlState.Normal) // Remove the knob image by setting it to nil, or add your own image.
Change the thumb tint to clear colour from storyboard
For Swift 5.0 or Later Version
for state: UIControl.State in [.normal, .selected, .application, .reserved] {
editorSlider.setThumbImage(UIImage.init(named: "slider_thumb"), for: state)
}

UIBarButtonItem ignores title attributes when in UIToolBar in iOS 8

I started a new master detail project in Xcode and I added a single UIBarButtonItem to the UIToolBar. In viewDidLoad I try and set some appearance attributes on the button.
self.button.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.redColor()], forState: .Normal)
self.button.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.greenColor()], forState: .Disabled)
The problem is that the attributes for the .Disabled state are never used, even when I set self.button.enabled = false. The button always uses the red color from the .Normal state.
What's stranger is that if I put the exact same button in the navigationBar then the disabled state works fine. What gives?

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