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! :)
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'.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.