1

I have a custom UIView class that creates a view I can use in my main view controller that allows for better dragging. The problem, is that right now, the object I'm dragging is just a black box. I would like to be able to put my own custom image into that box. How would I do this? The custom UIView only has a .h and a .m and I don't know which method to put the code in.

Here's the code Im using right now

UIImageView *player = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 37, 37)];
player.image = [UIImage imageNamed:@"ghost.png"];
[self addSubview: player];
vikingosegundo
  • 52,040
  • 14
  • 137
  • 178
Szwedo
  • 354
  • 1
  • 3
  • 15

1 Answers1

2

You could

  • Add your custom view in your view controllers main xib file and add the image view to it in the interface builder view

  • In your view controllers viewDidLoad method you could add the UIImageView to the custom view using your current code

  • Override the drawRect method of your custom view and draw the image there see here (however note the answer which says you should not load the image in the drawRect method, load the image on construction of your view in the initWithFrame or the awakeFromNib method depending on how you load it)

Community
  • 1
  • 1
Aran Mulholland
  • 23,555
  • 29
  • 141
  • 228