I'm changing the image of my button by using a boolean like this, but I have a couple of problems:
#IBOutlet weak var checkedButtonOutl: UIButton!
var hasBeenTouched = false
override func viewDidLoad() {
super.viewDidLoad()
checkedButtonOutl.setImage(uncheckedImage, forState: UIControlState.Normal)
checkedButtonOutl.addTarget(self, action: "click", forControlEvents: UIControlEvents.TouchUpInside)
checkedButtonOutl.showsTouchWhenHighlighted = false
}
func click(){
if hasBeenTouched == false{
checkedButtonOutl.setImage(uncheckedImage, forState: UIControlState.Normal)
hasBeenTouched = true
}else{
checkedButtonOutl.setImage(checkedImage, forState: UIControlState.Normal)
hasBeenTouched = false
Right after the view has loaded, you have to touch the button two times before it changes. Also, when the button is touched it highlights, which I don't want and I thought the showsTouchWhenHighlighted function would take care of.
Any suggestions on how to fix these two problems would be appreciated.
Related
I've created a custom button and set two images, one is for normal, and the other is for the selected mode. But the voice-over always says the normal image name text when the button is not selected. I've tried a lot but could not disable it.
When I disable the button imageView accessibility it is not working.
button.imageView?.isAccessibilityElement = false
When I disable the button accessibility, the voice-over is not working in accessibility mode.
button.isAccessibilityElement = false
If I remove the '.normal' mode image then it works, but normal mode image functionality is not considered/worked there. I'm surfing a lot. Help anyone and thanks in advance.
Code:
self.setImage(UIImage.init(named: imageName1), for: .normal)
self.setImage(UIImage.init(named: imageName1), for: .selected)
You can do it with a simple function, this is an example...
Declare your image and your button under controller class:
let newButton: UIButton = {
let button = UIButton(type: .system)
button.backgroundColor = .red
button.tintColor = .white
button.imageView?.contentMode = .scaleAspectFit
button.clipsToBounds = true
return button
}()
let image1 = UIImage(named: "magnifier") // image in my assets
let image2 = UIImage(named: "user") // image in my assets
in viewDidLoad addTarget to your button and call the control function, in my case:
handleCange()
newButton.addTarget(self, action: #selector(handleCange), for: .touchUpInside)
now set control variable and handleCange() func
var controlButtonState = false
#objc fileprivate func handleCange() {
if controlButtonState == true {
newButton.setImage(image1, for: .normal)
controlButtonState = false
} else {
newButton.setImage(image2, for: .normal)
controlButtonState = true
}
}
Basically, it is not possible indirect way. On the other hand we can use accessibilityLabel
I found an alternative solution. I think it is not a proper solution. Nonetheless, I am sharing this alternative solution. The question is open if anyone gets any proper solutions. Thanks!
import UIKit
struct RadioViewControllerConstant {
static let dayImage = "RadioButtonDontSelect"
static let dayImageSelected = "RadioButtonSelect"
}
class RadioViewController: UIViewController {
#IBOutlet weak var button1: UIButton!
#IBOutlet weak var button2: UIButton!
let image1 = UIImage(named: RadioViewControllerConstant.dayImageSelected)
let image2 = UIImage(named: RadioViewControllerConstant.dayImage)
var controlButtonState1 = false
var controlButtonState2 = false
override func viewDidLoad() {
super.viewDidLoad()
setVO()
}
func setVO() {
button1.accessibilityTraits = .none
button2.accessibilityTraits = .none
button1.isSelected = true
button2.isSelected = true
handleCange1()
handleCange2()
button1.addTarget(self, action: #selector(handleCange1), for: .touchUpInside)
button2.addTarget(self, action: #selector(handleCange2), for: .touchUpInside)
}
#objc fileprivate func handleCange1() {
if controlButtonState1 == true {
button1.imageView?.accessibilityLabel = "Radio button deselected"
button1.setImage(image2, for: .selected)
controlButtonState1 = false
} else {
button1.imageView?.accessibilityLabel = "Radio button selected"
button1.setImage(image1, for: .selected)
controlButtonState1 = true
}
}
#objc fileprivate func handleCange2() {
if controlButtonState2 == true {
button2.imageView?.accessibilityLabel = "Radio button deselected"
button2.setImage(image2, for: .selected)
controlButtonState2 = false
} else {
button2.imageView?.accessibilityLabel = "Radio button selected"
button2.setImage(image1, for: .selected)
controlButtonState2 = true
}
}
}
I have two images for a UIButton. In its notmal state, it's a pencil. When I select it, it's a paper. But right when I touch down it shows the pencil even though I haven't selected it yet. How can I prevent this?
#IBOutlet weak var btn: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
btn.setImage(#imageLiteral(resourceName: "Pencil").withRenderingMode(.alwaysTemplate), for: .normal)
btn.setImage(#imageLiteral(resourceName: "Paper").withRenderingMode(.alwaysTemplate), for: .selected)
btn.tintColor = .cyan
btn.addTarget(self, action: #selector(touchedButton), for: .touchUpInside)
}
func touchedButton() {
if !btn.isSelected {
btn.isSelected = true
} else {
btn.isSelected = false
}
}
u can use highlighted sate of UIButton for example, set image for highlighted state also,
btn.setImage(#imageLiteral(resourceName:
"Paper").withRenderingMode(.alwaysTemplate), for: .highlighted)
I'm going to implement a check box like this:
by
class CheckBox: UIButton {
let checkedImage = UIImage(named: "checked")
let uncheckedImage = UIImage(named: "unchecked")
var checked : Bool = false{
didSet{
if checked == false{
self.setImage(uncheckedImage, forState: .Normal)
}else {
self.setImage(checkedImage, forState: .Normal)
}
}
}
override func awakeFromNib() {
self.addTarget(self, action: "buttonClicked:", forControlEvents: .TouchUpInside)
checked = false
}
func buttonClicked(sender: UIButton){
if (sender == self){
checked = !checked
}
}
}
but everything I got is:
Could you explain what happened?
i tried your code it's works fine i only changed this part self.addTarget... to:
self.addTarget(self, action: #selector(CheckBox.buttonClicked(_:)), forControlEvents: .TouchUpInside)
and when you create the button change it class to: CheckBox
Call self.setNeedsLayout() at the end of your didSet statement.
If you change any of subviews you need to ask it to update it's layout and children. Let me know if that helped.
I want to hide the Label text of a button onClick and instead show an image.
On a second click, the title should appear again.
Sadly, the title disappears and the color of the button changes but there is no image shown and on click it never enters the "else"-Part of the if/else where the label should appear back again, so "2" is never printed.
What is the error?
if (button10.titleLabel!.text != "") {
print("1")
button10.setTitle("", forState: .Normal)
button10.setImage(UIImage(named: "1.png"), forState: UIControlState.Normal)
}
else if (button10.titleLabel!.text == ""){
print("2")
button10.setTitle("String", forState: .Normal)
}
You need to remove/change the image for the button too:
#IBOutlet weak var button: UIButton!
var clicked = false
#IBAction func buttonClicked(sender: AnyObject) {
if (clicked){
clicked = false
button.setTitle("", forState: .Normal)
button.setBackgroundImage(UIImage(named: "black"), forState: UIControlState.Normal)
}
else{
clicked = true
button.setBackgroundImage(UIImage(named: ""), forState: UIControlState.Normal)
button.setTitle("Clicked", forState: .Normal)
}
}
And to change the image for a UIButton use setBackgroundImage
When user taps "download" button i need to change the title of a button to
"loading..." but the number of dots must dynamically change from 1 to 3. How can i make it for certain amount of time(5-10 sec) ? Or do i just need to setTitle multiple times ?
button.setTitle("loading.", forState: .Normal)
You need to use
button.setTitle("loading.", forState: .Normal)
button.setTitle("loading..", forState: .Normal)
button.setTitle("loading...", forState: .Normal)
whenever you want to change the number of dots.
Call one of the above after a delay or whenever you need to update the button.
EDIT: To prevent your button's title from blinking (as pointed out by teamnorge) use:
UIView.performWithoutAnimation {
button.setTitle("loading...", forState: .Normal)
}
#IBAction func renameClassButton(sender: AnyObject) {
classTopButton.text = "\(classTopTextField)"
}
You can d it this way:
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var loading: UIButton!
//Create an array for your button name
let buttonNameArray = ["loading.","loading..","loading..."]
override func viewDidLoad() {
super.viewDidLoad()
}
#IBAction func loadingButton(sender: AnyObject) {
//Set delay time for changing name
var delayTime = 1.0
for name in buttonNameArray {
self.delay(delayTime++){
self.loading.setTitle(name, forState: .Normal)
}
}
}
//Function for delay
func delay(delay:Double, closure:()->()) {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW,Int64(delay * Double(NSEC_PER_SEC))),dispatch_get_main_queue(), closure)
}
}
And your result will be:
Hope this will help.
button.setTitle("my text here", forState: .Normal)