0

Background: I am creating a button with an image and a label. I add the label to the button with button.addSubview(labelName) and then I add the button to the main view with self.view.addsubview(buttonName). I am creating all views programatically and with UIKit. I do not use SwiftUI or Storyboard.

Problem: When pressing the button and holding the press, by default the button image automatically gets darker and loses color (I do not know what this animation or action is called, but I guess it's some kind of default button press/click action). The gif below shows this animation. The problem is, only the background image of the button is affected. The label and the label background, that I added to the button, are not effected by this dim animation.

enter image description here

What I want to achieve: I want the label and the label background to get the same dim effect, when the button is pressed. I don't like the way the label stands out with its bright colors while the button is pressed. Do I need to recognize the click and add some kind of dim animation to all childviews of the button manually or should I avoid adding labels to buttons like I did in the code below?

Simplified code for testing:

func doStuff(){
    let testbutton = UIButton(frame: CGRect(x: self.view.frame.width/4, y: 100, width: 200, height: 130))

    let testImg = UIImage(named: "rand.jpeg")
    testbutton.setImage(testImg, for: .normal)
    testbutton.addTarget(self, action: #selector(otherStuff), for: .touchUpInside)

    let label = UILabel(frame: CGRect(x: testbutton.bounds.origin.x+20, y: testbutton.bounds.origin.y+20, width: 150, height: 50))
    label.text = "NOT EFFECTED"
    label.backgroundColor = .red
    testbutton.addSubview(label)
    self.view.addSubview(testbutton)
}
SanCrusher
  • 37
  • 7
  • 1
    add this https://stackoverflow.com/questions/34744897/what-control-events-start-and-end-highlight-state-of-uibutton then apply alpha to the UILabel – ChanOnly123 Oct 02 '21 at 18:23
  • @ChanOnly123 thanks for your answer! While this works, I would love if the highlighting would be done automatically/by default, just like it is happening with the image of the button itself. This would guarantee that the alpha values are the same for the label and the button itself. With the version you linked, I would have to guess an alpha value manually which could differ from the button image. – SanCrusher Oct 02 '21 at 20:35
  • 2
    affected not effected – lorem ipsum Oct 02 '21 at 20:52
  • "I want the label and the label background to get the same dim effect" Applying the same dim effect on the label? Your question is a little bit far-fetched. You are merely setting an image to the button. It would make sense if you render the button image darker programmatically. – El Tomato Oct 03 '21 at 02:37
  • @El Tomato I just want the text on the label and the label background to be highlighted the same as the green image on the button. As shown in the gif, when I press the button the green image gets darker (automatically) but the text of the label and its red background stay the same even though they are a child of the button view!? – SanCrusher Oct 06 '21 at 01:06
  • Well, then, don't use `UILabel`. Set the title to the button instead. – El Tomato Oct 06 '21 at 01:57

0 Answers0