Setting a background image for UIButton

后端 未结 7 1221
孤城傲影
孤城傲影 2020-12-17 14:59

I am a newbie with Objective C. How would I set the background of this button to an image (image is already in the resources folder in XCode, \"blue_button.png\") instead o

7条回答
  •  我在风中等你
    2020-12-17 15:37

    You need to declare your button with the following UIButtonTypeCustom type:

    btnClear = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
    

    Then you should be able to use the follow:

    [btnClear setImage:[UIImage imageNamed:@"blue_button.png"] forState:UIControlStateNormal];
    

    There are 6 different control states that can be set.

    enum {
       UIControlStateNormal               = 0,
       UIControlStateHighlighted          = 1 << 0,
       UIControlStateDisabled             = 1 << 1,
       UIControlStateSelected             = 1 << 2,
       UIControlStateApplication          = 0x00FF0000,
       UIControlStateReserved             = 0xFF000000
    };
    

    Here are some references that may help:

    UIButton Class Reference

    UIControl Class Reference

提交回复
热议问题