is it possible to update UIButton title/text programmatically?

后端 未结 12 1787
北海茫月
北海茫月 2020-12-07 19:58

I have a UIButton, that when pressed, brings up a new view where the user can change some settings. When the view is dismissed, I\'d like to update the title/t

12条回答
  •  南方客
    南方客 (楼主)
    2020-12-07 20:10

    Even though Caffeine Coma's issue was resolved, I would like to offer another potential cause for the title not showing up on a UIButton.

    If you set an image for the UIButton using

    - (void)setImage:(UIImage *)image forState:(UIControlState)state
    

    It can cover the title. I found this out the hard way and imagine some of you end up reading this page for the same reason.

    Use this method instead

    - (void)setBackgroundImage:(UIImage *)image forState:(UIControlState)state
    

    for the button image and the title will not be affected.

    I tested this with programmatically created buttons and buttons created in a .xib

提交回复
热议问题