0

I can't believe I cannot figure this out. After migrating to Swift 3 yesterday I got everything but one thing working.

I created a custom button subclassing from UIButton. Upon selection I want to change the background color. This worked in Swift 2.2. Now I cant change the backgroundcolor when the button's been pressed. I can still change the background color of a UIButton, but not on the subclassed button.

What changed in Swift 3, am I missing something? The color for setTitleColor does change by the way.

Code:

func selectButton(_ button: CustomButton) {
    button.isSelected = true
    button.backgroundColor = UIColor.red
    button.setTitleColor(UIColor.white, for: .selected)
}

When I add printstatements, I see that the output is different, but the view (color) hasn't changed.

print("CHANGE COLOR FROM: \(button.backgroundColor)")
button.backgroundColor = UIColor.red
button.setTitleColor(UIColor.white, for: .selected)
button.setNeedsDisplay()
print("CHANGE COLOR TO: \(button.backgroundColor)")

Output:

CHANGE COLOR FROM: Optional(UIExtendedGrayColorSpace 1 1)
CHANGE COLOR TO: Optional(UIExtendedSRGBColorSpace 1 0 0 1)
0xT0mT0m
  • 656
  • 5
  • 15
  • a little code snippet, maybe? I have tens of designable `UIViews` (incl. `UIButtons` as well), I did the migration just yesterday, and none of the desinbable views were broken; so it would be great to see what you have done, because that issue does not seem to be a generic one. – holex Sep 15 '16 at 08:43
  • Hmm, thats good news (it's still possible) and bad (I seem to be doing something wrong). Ill edit the question. – 0xT0mT0m Sep 15 '16 at 08:48
  • is it the real code? how can be sure `Color.bePrEPared.red()` is not `nil`? – holex Sep 15 '16 at 08:51
  • The crazy thing is, the titleColor does change. – 0xT0mT0m Sep 15 '16 at 08:51
  • A regular UIColor doesn't work either. Ill change for clarification – 0xT0mT0m Sep 15 '16 at 08:51

1 Answers1

0

I actually found that the problem was setting cornerRadius was somehow messing up my button. Removing the cornerRadius or changing it in viewDidAppear fixed the problem.

Also described in cornerRadius stopped working in Swift 2.3 / iOS 10 / Xcode 8

Community
  • 1
  • 1
0xT0mT0m
  • 656
  • 5
  • 15