当ViewController添加为子视图时,UIImageView不会显示

时间:2014-05-20 19:38:21

标签: objective-c xcode cocoa-touch uiimageview subview

我的主要目标是为 我的所有ViewControllers 设置一个背景。每个ViewController我都有明确的背景。

为此,我制作了一个UIViewController(称为backgroundViewController),作为我subview ViewControllers的{​​{1}}。它有一个显示此特定背景的UIImageView。然后,我会将此backgroundViewController添加为我所有其他subview的{​​{1}}。

问题是 - 这个ViewControllers不会显示为子视图!

这是我显示imageView

的方式
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]; } } 都会发生变化。

为了测试backgroundView确实显示为子视图,我添加了另一个imageView并将其图像更改为Interface Builder中的静态.png,并且正确显示为子视图。 / p>

这就是我将其作为其他backgroundViewController的子视图的方式:

ViewControllers

我想要的是backgroundViewController *backgroundVC = [[backgroundViewController alloc] initWithNibName:@"backgroundViewController" bundle:nil]; [self.view insertSubview:backgroundVC.view atIndex:0]; 名为UIImageView,当它被称为子视图时会显示出来。

我已经测试backgroundView根据播放的歌曲确实发生了变化,并且它可以正常工作。

我做错了什么? backgroundView拒绝显示为backgroundView ?!我已经大量搜索了subview ViewControllers,但我找不到同样的问题。

非常感谢任何帮助,谢谢! :)

1 个答案:

答案 0 :(得分:0)

这对我有用:

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

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

我找到了解决方案here。我需要阅读更多有关“自定义容器”的信息。

相关问题