Change button color in swift - iphone

How to change button color, while button is pressed. Like in calculator app. When finger is out button, it should return to default color. Thanks.

For your button, change the color from your default color to a highlighted color for TouchDown control event and nevert the colors back to your default color on the TouchCancel and TouchDragExit control events.
button.addTarget(self, action: "changeButtonColors:", forControlEvents: .TouchDown)
button.addTarget(self, action: "revertButtonColors:", forControlEvents: .TouchCancel)
button.addTarget(self, action: "revertButtonColors:", forControlEvents: .TouchDragExit)
func changeButtonColors(button: UIButton) {
button.layer.backgroundColor = UIColor.greenColor().CGColor
button.setTitleColor(UIColor.whiteColor(), forState: .Normal)
}
func revertButtonColors(button: UIButton) {
button.layer.backgroundColor = UIColor.whiteColor().CGColor
button.setTitleColor(UIColor.greenColor(), forState: .Normal)
}

Related

Swift/Xcode - No response from button in custom annotation view

I have created a custom annotation view, as described here: https://sweettutos.com/2016/03/16/how-to-completely-customise-your-map-annotations-callout-views/
It works fine and displays the correct data/info.
The problem seems to be that there is no respons to .touchUpInside from the added button.
let button = UIButton()
button.addTarget(self, action: #selector(ViewController.deleteFromAnnot(sender: )), for: .touchUpInside)
button.frame = CGRect.init(x: 16, y: 543, width: 268, height: 30)
button.backgroundColor = .yellow
calloutView.addSubview(button)
#objc func deleteFromAnnot(sender: UIButton)
{
print("Do something")
}
It looks to me like you have set your button target incorrectly.
Replace
button.addTarget(self, action: #selector(ViewController.deleteFromAnnot(sender: )), for: .touchUpInside)
With
button.addTarget(self, action: #selector(deleteFromAnnot(sender: )), for: .touchUpInside)

Removing a UILabel programmatically after a button is pressed

I am trying to create/show a label when a button is pressed, and then delete/hide the same label when the same button is pressed again. I am trying to do this all programmatically in Swift.
I have tried using label.removeFromSuperview() but it doesn't seem to have any effect. However when i try removing the button in the same code location using button.removeFromSuperview()
var label = UILabel()
let labelImage = UIImage(named: "Strike Line.png")
/* to select checkmarked state */
func pressCheck() {
let image = UIImage(named: "Checkmark.png")
button.setBackgroundImage(image, for: UIControlState.normal)
button.addTarget(self, action:#selector(self.pressUnCheck), for: .touchUpInside)
self.view.addSubview(button)
textField1.textColor = UIColor.gray //change textfield to a gray color
label = UILabel(frame: CGRect(x : 31, y : 69, width: 200, height: 2))
label.backgroundColor = UIColor(patternImage: labelImage!)
self.view.addSubview(label)
}
func pressUnCheck()
{
let image = UIImage(named: "To Be Completed Circle.png")
button.setBackgroundImage(image, for: UIControlState.normal)
button.addTarget(self, action:#selector(self.pressCheck), for: .touchUpInside)
self.view.addSubview(button)
label.removeFromSuperview()
textField1.textColor = UIColor.black
}
Here is where i am trying to remove/hide the label.
Since this apparently was the fix I'll drop it in as an answer.
Add button.removeTarget(nil, action: nil, for: .allEvents) before you add any new targets to your button.
If you don't remove the current target it'll have multiple targets and be calling both pressCheck() and pressUnCheck() on each button press.
There's a few ways to handle this... If you just want to hide it you can use
label.isHidden = true - would hide the label.
label.isHidden = false - would show the label.

tvOS Button Action

I create a UIButton in tvOS via Swift
let randomBtn = UIButton()
randomBtn.setTitle("Zufällig", forState: .Normal)
let RndNormal = UIImage(named: "RndNormal")
let RndHoover = UIImage(named: "RndHoover")
randomBtn.setImage(RndNormal, forState: .Normal)
randomBtn.setImage(RndHoover, forState: .Focused)
randomBtn.setTitleColor(UIColor.blackColor(), forState: .Normal)
randomBtn.setTitleColor(UIColor.whiteColor(), forState: .Focused)
randomBtn.addTarget(self, action: #selector(ViewController.click(_:)), forControlEvents: UIControlEvents.TouchUpInside)
let screenSize: CGRect = UIScreen.mainScreen().bounds
let screenWidth = screenSize.width
randomBtn.frame = CGRect(x: screenWidth - 150, y: 60 , width: 70 , height: 70)
self.view.addSubview(randomBtn)
But the action never get fired if I press the button, is there anything different in tvOS?
func click(sender: UIButton) {
print("click")
}
In tvOS for Button action UIControlEvents TouchUpInside will not call.
You have to use UIControlEvents PrimaryActionTriggered like below.
randomBtn.addTarget(self, action: #selector(ViewController.click(_:)), forControlEvents: UIControlEvents. PrimaryActionTriggered)
Also Refer this link if you have any confusion
https://forums.developer.apple.com/thread/17925
randomBtn.addTarget(self, action: #selector(ViewController.click(_:)), forControlEvents: UIControlEvents.PrimaryActionTriggered)
Solved it, its just a other UIControlEvents

Disabling a button in and turning it to grey

I got a button that opens a new view once pressed.
the button is called " cálculo "
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let secondViewController:ContasView = segue.
destinationViewController as! ContasView
I don't want it to be enable to be pressed until a label value is not nil. I usually see this kind of button as a grey text button.
So, two questions:
1. How to disable it be this condition?
2. As soon as it's disabled, how to turn into a grey text button?
Start listening:
textField1.addTarget(self, action: "textFieldDidChange:", forControlEvents: UIControlEvents.EditingChanged)
textField2.addTarget(self, action: "textFieldDidChange:", forControlEvents: UIControlEvents.EditingChanged)
textField3.addTarget(self, action: "textFieldDidChange:", forControlEvents: UIControlEvents.EditingChanged)
Check values:
func textFieldDidChange(textField: UITextField) {
button.enabled = !textField1.text.isEmpty && !textField2.text.isEmpty && !textField3.text.isEmpty
var button = UIButton(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
button.userInteractionEnabled = false
button.setTitle("ButtonTextHere", forState: .Normal)
button.setTitleColor(UIColor.grayColor(), forState: .Normal)
view.addSubview(button)
Somewhere in your code, whenever your label changes, you can just do
button.userInteractionEnabled = true
button.setTitleColor(UIColor.blueColor(), forState: .Normal)

Swift- How to Display Image over Button?

I'm trying to display an Image over a button, I'm getting BLUE PRINT of Image over button, I have tried so far this-
override func viewDidLoad()
{
super.viewDidLoad()
var birdTexture1 = UIImage(named: "fish.png") as UIImage
button1.frame = CGRectMake(100, 10, 50, 150)
button1.setImage(birdTexture1, forState: .Normal)
button1.setTitle("Testing", forState: UIControlState.Normal)
button1.addTarget(self, action: "btnAction:", forControlEvents: UIControlEvents.TouchUpInside)
self.view.addSubview(button1)
// Do any additional setup after loading the view, typically from a nib.
}
By above code, I m getting, the following output
But the Actual Image is this-
I think your UIButton needs to be of type UIButtonTypeCustom, rather than UIButtonTypeSystem. You either constructed it as such, or need to update the UIButton's properties in interface builder.
More info: https://developer.apple.com/library/ios/documentation/uikit/reference/UIButton_Class/UIButton/UIButton.html#//apple_ref/occ/clm/UIButton/buttonWithType:
i think replace this code:
let birdTexture1 = UIImage(named: "fish.png")! as UIImage
let button1:UIButton=UIButton(frame:CGRectMake(100, 50, 50, 150))
button1.setBackgroundImage(birdTexture1, forState: .Normal)
button1.setTitle("Testing", forState: .Normal)
button1.addTarget(self, action: #selector(ViewController.btnActionClick(_:)), forControlEvents: UIControlEvents.TouchUpInside)
self.view.addSubview(button1)
#IBAction func btnActionClick(sender:AnyObject){
}
and also add to image in target