Disabling a button in and turning it to grey - swift

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)

Related

The action in addTarget of my button does not work

Xcode did not report errors but nothing was printed when I pressed the button.
I tried it with action:
#selector (print), action: #selector (self.print), action: #selector (ViewController.print)..etc..
Swift 4, Xcode 10.1
class SomeViewController:UIViewController {
var button:UIButton = UIButton()
func setupButton()
{
button = UIButton(frame: CGRect(x: 140, y: 140, width: 90, height: 40))
button.addTarget(self, action: #selector(print(-:)) , for: .touchUpOutside)
navigationController?.navigationBar.addSubview(button)
button.setTitle("Convert", for: .normal)
button.backgroundColor = .yellow
view.addSubview(button)
}
#objc func print(_sender: UIButton)
{
print("Stackoverflow")
}
override func viewDidLoad()
{
super.viewDidLoad()
view.addSubview(CollectionView)
CollectionView.frame = view.frame
setupButton()
}
}
You just have to change event type from .touchUpOutside to .touchUpInside
Here is events documentation
Change this ( You need touchUpInside instead of touchUpOutside )  
button.addTarget(self, action: #selector(print(_:)) , for: .touchUpOutside)
to
button.addTarget(self, action: #selector(printRes) , for: .touchUpInside)
#objc func printRes(_ sender: UIButton) { }
Also don't use print as button action name because it's a reserved method
Also to get the button click action set it's type
button = UIButton(type: .system)
button.frame = CGRect(x: 140, y: 140, width: 90, height: 40)

Add a button load more below uitableview

I need a solution in swift to add a button below uitableview, how to do it ? do I have to add a custom cell at the end ? or add an another view below uitableview ? I tried to add a view, but I can't see this one.
Here is the simplest way to add a button/CustomView at the bottom of the tableView.
override func viewDidLoad() {
super.viewDidLoad()
let button = UIButton(frame: CGRect(origin: .zero, size: CGSize(width: self.tableView.frame.width, height: 40)))
button.setTitle("Load more", for: .normal)
button.backgroundColor = .lightGray
button.addTarget(self, action: #selector(moreButtonClicked(_:)), for: .touchUpInside)
self.tableView.tableFooterView = button
}
#objc func moreButtonClicked(_ sender: UIButton) {
print("More button clicked, fetch more data!")
}

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

Change button color in swift

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)
}

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