UIbutton is not highlighting when pressed

前端 未结 2 1940
你的背包
你的背包 2021-01-07 20:33

My UIButton instance is not highlighting when pressed. I\'m pretty sure my code is correct. This only happens in this specific class that I am using. I\'m guess

相关标签:
2条回答
  • 2021-01-07 21:00

    Your code is correct except this line let backButton = UIButton(). Here you need to say button Type. Either you can init with Frame or Button Type

    0 讨论(0)
  • 2021-01-07 21:13

    Initialize the button using UIButton(type buttonType: .system) instead of UIButton().

    When you use the latter it won't properly set up button highlighting because the default button type is UIButtonType.custom, which is intended as a blank slate without any of the default styling or highlighting behaviour.

    Note, from Apple's UIButton documentation:

    A button’s type defines its basic appearance and behavior. You specify the type of a button at creation time using the init(type:) method or in your storyboard file. After creating a button, you cannot change its type. The most commonly used button types are the Custom and System types, but use the other types when appropriate.

    0 讨论(0)
提交回复
热议问题