Setting a background image for UIButton

后端 未结 7 1194
孤城傲影
孤城傲影 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:42

    This works:

    UIButton *btnClear = [[UIButton alloc] init];
    btnClear = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
    btnClear.frame = CGRectMake(115, 200, 90, 40);
    [btnClear setTitle:@"Clear" forState:UIControlStateNormal];
    [btnClear setBackgroundImage:[UIImage imageNamed:@"blue_button.png"] forState:UIControlStateNormal];
    [btnClear addTarget:self action:@selector(clearAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btnClear];
    

提交回复
热议问题