I'm new to Swift and to stackoverflow so please comment for any improvements. I can make to my asking of questions!
I have created a UIButton that has an image. When tapped, the image is supposed to change to another image. Currently, once tapped, it 'flashes' with the image it is supposed to change to and then immediately changes back to the original image.
I created an outlet:
#IBOutlet var buttonImage: UIButton!
Then I added this:
override func viewDidLoad() {
super.viewDidLoad()
//Change image.
buttonImage.setImage(UIImage(named:"image1.png"),forState:UIControlState.Normal)
buttonImage.setImage(UIImage(named:"image2.png"),forState:UIControlState.Highlighted)
}
Help is gratefully received.
EDIT - added error screenshot.errorscreenshot
You need to change your viewDidLoad code like this
buttonImage.setImage(UIImage(named:"image1.png"),forState:UIControlState.Normal)
buttonImage.setImage(UIImage(named:"image2.png"),forState:UIControlState.Selected)
Now change you IBAction like this
#IBAction func btnTap(sender: UIButton) {
sender.selected = !sender.selected
}
Hope this will help you.
You need to assign two images for the button like the screenshots below.
As you can see I have state config as Default and Selected.
Later on the action of button you can do
#IBAction btnTapAction(sender: UIButton) {
if sender.selected{
sender.selected = false
}
else{
sender.selected = true
}
}
Related
We want Lyft button touch event because I am working in analytics, so, I need how many people choose Lyft but I can't put UIView click event. I try below code.
let gesture = UITapGestureRecognizer(target: self, action: #selector(self.checkAction))
cell.lyftButton.addGestureRecognizer(gesture)
How can i achieve this?
You can directly assign a selector method to lyftButton e.g
lyftButton.addTarget(self, action: #selector(lyftButtonAction(_:)), for: .touchUpInside)
#objc
func lyftButtonAction(_sender: UIButton) {
//Do your action
}
To retrieve the LyftButton, you'll need to fetch the button inside the Lyft view, after retrieving it, I tried to add another target to it which was your 'checkAction' method, but for some reason it is not being called. One workaround solution is:
On Auto Layout, created a transparent button on top of the Lyft Button View, let's callet it 'Transparent Lyft Button': Example (I've embeded in another view because it was on a stackView);
On the code, retrieved the button with the above method, held it in a variable, let's call it 'requestLyftButton' and disabled it.
Created an IBAction for the 'Transparent Lyft Button' that triggers the method 'self.checkAction' that you've created and also calls requestLyftButton.sendActions(for: .touchUpInside), which triggers the original Lyft SDK action.
To Retrieve Lyft UIButton:
#IBOutlet weak var lyftButton: LyftButton!
#IBOutlet weak var transparentLyftButton: UIButton!
var requestLyftButton: UIButton?
func retrieveLyftButton(in view: UIView) {
for view in view.subviews {
if let lyftBtn = view as? UIButton {
lyftBtn.isEnabled = false
requestLyftButton = lyftBtn
} else {
retrieveLyftBtn(in: view)
}
}
}
transparentLyftButton IBAction to trigger your method + lyft sdk original action:
#IBAction func requestLyft(_ sender: UIButton) {
if let lyftBtn = requestLyftButton {
checkAction() // Your method
lyftBtn.sendActions(for: .touchUpInside)
}
}
I hope that you can understand what was done, if you have any questions, just let me know.
I have a form that should be completed from the user, which is split between 3 UITabBarItems in 1 UITabBarController. I would like to change UITabBarItem's image dynamically. For example, the user is on the first step and right after he completes the configuration needed on that step I want the UITabBarItem that is responsible for this UIViewController to change its image to a tick indicating that the user can proceed to step two
I tried to set the tabBarItem in the current view controller to an image when all the values are completed, but it didn't work
if manufacturerCompleted
&& modelCompleted
{
let image = UIImage(named: "tick")
self.tabBarItem.image = image?.withRenderingMode(.automatic)
}
Thank you in advance! :)
Personally, I don't see any mistakes in the code. Maybe it doesn't get executed?
I suppose, you don't have an #IBAction function which gets called on the text change event (or whatever it is called). Try setting up one, connect it to the storyboard and then try again.
Oh, and a more 'swift-ey' way to handle optionals is the 'optional binding'. See more here.
var manufacturerCompleted = false
var modelCompleted = false
#IBAction func handleKeyDown(_ sender: UITextField) {
updateManufacturerComplete(sender)
updateModelComplete(sender)
if manufacturerCompleted && modelCompleted {
if let image = UIImage(named: "tick") as UIImage? {
self.tabBarItem.image = image.withRenderingMode(.automatic)
}
}
}
func updateManufacturerComplete(_ sender: UITextField) {
// Your condition here
// ...
self.manufacturerCompleted = true
}
func modelCompleted(_ sender: UITextField) {
// Your condition here
// ...
self.modelCompleted = true
}
I'm new to Swift and I assume this is a fundamental question to programming for iOS.
I have three buttons in my storyboard and I want to customize how those buttons look if pressed once, twice and three times.
I also have three themes (pink, blue and orange). What I thought of doing is to create three new classes called pink,blue and orange.swift
I don't want to create them programmatically, only style them programmatically.
What I lack to understand is how do I call the function (Example: "ButtonIsPressed") from my pink.swift class into my #IBAction and #IBOutlet in the main view controller that is also object oriented (ie. I don't want to create a function for every button)?
I can't really find a decent and up-to-date Swift 3 Tutorial for this, any help or advice on this topic will be greatly appreciated.
Why can it not be as simple as?:
#IBAction func buttonPressed(_ sender: UIButton!) {
self.backgroundColor = myPinkCGolor
}
I think shallowThought's answer will work for changing backgroundColor based on button state of a specifically named IBOutlet.
I have three buttons in my storyboard and I want to customize how those buttons look if pressed once, twice and three times.
If you want to maintain "state", as in have a "counter" for how many times a button's been clicked or tapped, you can use the "tag" property of the button. Set it to zero, and in your IBAction functions increment it. (Like shallowThought said, use .touchUpInside and .touchDown for the events.)
Also, you have one minor - but important! - thing wrong in your code Brewski:
#IBAction func buttonPressed(_ sender: UIButton!) {
self.backgroundColor = myPinkCGolor
}
Should be:
#IBAction func buttonPressed(_ sender: UIButton!) {
sender.backgroundColor = myPinkCGolor
}
So combining everything - up vote to shallowThought (also, changing his AnyObject to UIButton and making it Swift 3.x syntax on the UIColors - and would end up with this. Note that there is no need for an IBOutlet, and you can wire everything up in IB without subclassing:
// .touchUpInside event
// can be adapted to show different color if you want, but is coded to always show white color
#IBAction func buttonClicked(sender: UIButton) {
sender.backgroundColor = UIColor.whiteColor()
}
// .touchDown event
// will show a different color based on tap counter
#IBAction func buttonReleased(sender: UIButton) {
switch sender.tag {
case 1:
sender.backgroundColor = UIColor.blue
case 2:
sender.backgroundColor = UIColor.red
case 3:
sender.backgroundColor = UIColor.green
default:
sender.backgroundColor = UIColor.yellow
}
sender.tag += 1
}
There is no methode to set the backgroundColor for a certain state, like there is for other UIButton properties, so you have to listen to the buttons actions:
class ViewController: UIViewController {
#IBOutlet weak var button: UIButton!
#IBAction func buttonClicked(sender: AnyObject) { //Touch Up Inside action
button.backgroundColor = UIColor.whiteColor()
}
#IBAction func buttonReleased(sender: AnyObject) { //Touch Down action
button.backgroundColor = UIColor.blueColor()
}
...
}
or set a unicolor image withimage:UIImage, forState:.selected.
Basically, I have a three buttons on my main screen.
When I select one of these buttons, I would like the text on the selected button to change to bold and change color (blue).
When I select a different button, I would like the newly selected button to change to bold and change color(blue), and the previously selected button to go back to normal. (non-bold and black text)
I have these buttons sending an action to the script.
This is what I have, I can't seem to get it to work. Help would be much appreciated!
#IBAction func buttonOne(sender: UIButton){
sender.setTitleColor(UIColor.blueColor(), forState: UIControlState.Highlighted)
}
I have tried .Highlighted and .Selected on the UIControlState, neither seem to work. I have also tried the following, but I cant get it to work.
#IBAction func buttonOne(sender: UIButton){
sender.titleLabel?.textColor = UIColor.blueColor()
}
I figured that since the sender was a UIButton, and it was the button that was clicked, taking the values off of it and resetting them would work. I do believe I am missing something.
Thank you
Sounds like you want UIControlState.Normal
Selected does nothing in most cases, and Highlighted is only while you're pressing the button.
See more info here: https://developer.apple.com/library/ios/documentation/uikit/reference/uicontrol_class/index.html#//apple_ref/doc/constant_group/Control_State
Maybe you can do with a transform...
#IBAction func buttonPressed(sender: UIButton) {
sender.titleLabel!.textColor = UIColor.blueColor()
sender.transform = CGAffineTransformMakeScale(0.8, 0.8)
}
#IBAction func buttonReleased(sender: UIButton) {
sender.titleLabel!.textColor = UIColor.redColor()
sender.transform = CGAffineTransformIdentity
}
Provide tags to all buttons, connect each button actions to same function and try the following code:
#IBAction func butnClicked (sender : UIButton) {
for tg in 1...2 {
print(sender.tag)
let tmpButton = self.view.viewWithTag(tg) as? UIButton
if tmpButton?.tag == sender.tag {
tmpButton?.setTitleColor(.red, for: .normal)
} else {
tmpButton?.setTitleColor(.gray, for: .normal)
}
}
Hope will be helping.
i'm fairly new to swift and programming and have a very simple question.
I have four buttons with custom images in the UI and i'm trying to put a surrounding Graphic behind the pressed Button. Here's the line of code:
#IBAction func moodButton(sender: AnyObject) {
timerSelection.center = CGPointMake(moodButton.center.x, moodButton.center.y)
}
timerSelection is the UIImageView i'm trying to put to the coordinates of the selected button...
Please Help! & Thanks in advance :)
Try something like this:
#IBAction func moodButton(sender: UIButton) {
timerSelection.center = sender.center
}
You can send this subview to back before:
view.sendSubviewToBack(timerSelection)