Selected UIButton disappears inside selected UITableViewCell

前端 未结 5 1810
一个人的身影
一个人的身影 2021-01-06 00:39

I have a table, I have cellForRowAtIndexPath delegate for it and I have instance of UITableViewCell being created inside that method, which I return. Somewhere in cellForRow

5条回答
  •  旧巷少年郎
    2021-01-06 01:25

    The trick is to add UIButton as a subview onto UIImageView.

    1. Create UIImage

      UIImage backgroundImg = [UIImage imageName:@"yourimg"];
      UIImageView imageView = [UIImageView alloc]initWithImage:backgrounding];
      
    2. Now create the button

      UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
      // set the frame. the position of the button is the center 
      CGRect newIconRect = CGRectMake(imageView.center.x, imageView.center.y, 29, 29);
      
      button.frame = newIconRect;
      
    3. Finally add the button onto UIImageView

      [imageView addSubview:button];
      [cell addSubview imageView];
      

    Now you dont have to play with the highlight color.

提交回复
热议问题