Set a button background image iPhone programmatically

后端 未结 8 425
无人及你
无人及你 2020-12-13 14:25

In Xcode, how do you set the background of a UIButton as an image? Or, how can you set a background gradient in the UIButton?

8条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-13 15:08

    When setting an image in a tableViewCell or collectionViewCell, this worked for me:

    Place the following code in your cellForRowAtIndexPath or cellForItemAtIndexPath

    // Obtain pointer to cell. Answer assumes that you've done this, but here for completeness.

    CheeseCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cheeseCell" forIndexPath:indexPath];
    

    // Grab the image from document library and set it to the cell.

    UIImage *myCheese = [UIImage imageNamed:@"swissCheese.png"];
    [cell.cheeseThumbnail setImage:myCheese forState:UIControlStateNormal];
    

    NOTE: xCode seemed to get hung up on this for me. I had to restart both xCode and the Simulator, it worked properly.

    This assumes that you've got cheeseThumbnail set up as an IBOutlet... and some other stuff... hopefully you're familiar enough with table/collection views and can fit this in.

    Hope this helps.

提交回复
热议问题