0

My main aim is to have one background for all of my ViewControllers. Every ViewController I have has a clear background.

To do this, I have made one UIViewController (called backgroundViewController) that will act as the subview to all my other ViewControllers. It has one UIImageView which displays this particular background. I will then add this backgroundViewController as a subview of all my other ViewControllers.

The problem is - this imageView won't show as a subview!

This is how I display the imageView:

- (void)viewDidLoad
{


    if ([musicPlayer playbackState] == MPMusicPlaybackStateStopped) {

        UIImage *artworkBackgroundView;

        artworkBackgroundView = [UIImage imageNamed:@"noArtworkBackground"];

        UIImage *effectImage = nil;

        backgroundView.image = effectImage;

        [backgroundView setImage:artworkBackgroundView];
        backgroundView.image = effectImage;
    }
}

- (void) handle_NowPlayingItemChanged: (id) notification
{

    if ([musicPlayer playbackState] != MPMusicPlaybackStateStopped) {

// Get artwork for current now playing item
        MPMediaItem *currentItem = [musicPlayer nowPlayingItem];

        MPMediaItemArtwork *artwork = [currentItem valueForProperty: MPMediaItemPropertyArtwork];

        UIImage *artworkBackgroundView = [artwork imageWithSize: CGSizeMake(618, 618)];

        if (!artworkImage) {
            artworkBackgroundView = [UIImage imageNamed:@"noArtworkBackground"];
            artworkImage = [UIImage imageNamed:@"noArtwork"];
        }

        [backgroundView setImage:artworkBackgroundView];            
    }
}

As you can see, backgroundView changes each time the music player skips song.

To test that backgroundViewController does show as a subview, I added another imageView and changed its image to a static .png in Interface Builder, and that shows correctly as a subview.

This is how I make it a subview for other ViewControllers:

backgroundViewController *backgroundVC = [[backgroundViewController alloc] initWithNibName:@"backgroundViewController" bundle:nil];

[self.view insertSubview:backgroundVC.view atIndex:0];

What I want is the UIImageView called backgroundView to show up when it is being called as subview.

I have tested that backgroundView does change according to what song is playing, and it works correctly.

What am I doing wrong? backgroundView refuses to show up as a subview?! I've searched a ton about adding ViewControllers as subviews but I can't find a similar problem.

Any help would be much appreciated, thanks! :)

WunDaii
  • 2,322
  • 4
  • 18
  • 26
  • How is your backgroundVC and backgroundView connected? Instead of using a VC for background use just an image view. – nprd May 20 '14 at 22:14
  • @NaveenPrasadR How would I do this? I have never subclassed UIImageView and I have read that it is bad practice to do so - http://stackoverflow.com/questions/11719409/creating-subclass-of-uiimageview – WunDaii May 20 '14 at 22:44
  • Why you need a backgroundVC for background? Why not just UIImageView? You need not subclass an image view, just instantiate and add it to your view controllers instead of instantiating a BackGroundViewController? – nprd May 20 '14 at 23:20
  • @NaveenPrasadR , how would I do this? I have never done this before :S – WunDaii May 20 '14 at 23:20

1 Answers1

0

This worked for me:

backgroundViewController *backgroundVC = [[backgroundViewController alloc] initWithNibName:@"backgroundViewController" bundle:nil];

[self addChildViewController:backgroundVC];
[self.view addSubview:backgroundVC.view];

I found the solution here. I need to read more about 'Custom Containers'.

Community
  • 1
  • 1
WunDaii
  • 2,322
  • 4
  • 18
  • 26