I have an iPhone game that I’m trying to create for school, yet I’m having some trouble. I’ve created an animated UIImage that I’m using as my background. The problem is, that’s the farthest that I’ve got. I can’t display any buttons, text, or labels above the ImageView. I have it set in the View controller to where the ImageView is at the bottom so that all of the other objects can appear above. Does anyone have any idea what I’m doing wrong here or what I could add that could possibly fix this? I’ve included the code that creates the animation.
- (void)viewDidLoad
{
// create the view that will execute our animation
UIImageView* YourImageView = [[UIImageView alloc] initWithFrame:self.view.frame];
// load all the frames of our animation
YourImageView.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:@"Star-Field 1.png"],
[UIImage imageNamed:@"Star-Field 2.png"],
[UIImage imageNamed:@"Star-Field 3.png"],
[UIImage imageNamed:@"Star-Field 4.png"],
[UIImage imageNamed:@"Star-Field 5.png"],
[UIImage imageNamed:@"Star-Field 6.png"],
[UIImage imageNamed:@"Star-Field 7.png"],
[UIImage imageNamed:@"Star-Field 8.png"],
[UIImage imageNamed:@"Star-Field 9.png"],
[UIImage imageNamed:@"Star-Field 10.png"],
[UIImage imageNamed:@"Star-Field 11.png"],
[UIImage imageNamed:@"Star-Field 12.png"],
[UIImage imageNamed:@"Star-Field 13.png"],
[UIImage imageNamed:@"Star-Field 14.png"],
[UIImage imageNamed:@"Star-Field 15.png"],
[UIImage imageNamed:@"Star-Field 16.png"],
[UIImage imageNamed:@"Star-Field 17.png"],
[UIImage imageNamed:@"Star-Field 18.png"], nil];
// all frames will execute in 1.75 seconds
YourImageView.animationDuration = 0.5;
// repeat the animation forever
YourImageView.animationRepeatCount = 0;
// start animating
[YourImageView startAnimating];
// add the animation view to the main window
[self.view addSubview:YourImageView];
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
When instantiating view controllers, the views in the XIB file get instantiated first. After this, the
viewDidLoadmethod gets called.You are instantiating a new image view in your
viewDidLoad, this image view is added on top of every subview, which is why your buttons, labels, etc do not show up.To fix this, you can instruct the view controller’s view to send the new image view to the back, like this:
If I understand your description correctly, you have placed an image view to serve as your background in your XIB as well. You can access this instead of creating a new image view in the
viewDidLoadmethod. To do this, you have to declare anIBOutletproperty for the image view in your XIB file.