Mahir
Mahir

Reputation: 1684

Problem adding image to UIView

I'm making an app with a piano and each key will be it's own subclass.

I tried making the keys a subclass of UIButton, but after having some problems getting the methods to be called, I checked online and found out that others were also having problems subclassing UIButtons.

So then I tried subclassing a UIView. The problem was that I tried adding an image to it, but it showed up as a black box.

Here is the code from the CustomView

@implementation Keys

- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {

    self.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"whitekey.gif"]];                                

}
return self;

(I've confirmed that the problem isn't whitekey.gif)

Here is the code from the viewController

- (void)viewDidLoad
{
[super viewDidLoad];


Keys *whiteKey = [[Keys alloc] initWithFrame:CGRectMake(0, 0, 100, 300)];
[self.view addSubview:whiteKey];

}

Is it better if I subclass UIImageView?

Upvotes: 0

Views: 411

Answers (1)

csano
csano

Reputation: 13716

You should be using UIImageView.

An image view object provides a view-based container for displaying either a single image or for animating a series of images.

http://developer.apple.com/library/ios/#documentation/uikit/reference/UIImageView_Class/Reference/Reference.html

Maybe a UIView as a container, with a UIImageView with the piano key image, with additional UIViews on top of that, depending on what you need to do?

Upvotes: 2

Related Questions