[英]UIImageView Won't Show When ViewController Added As Subview
My main aim is to have one background for all of my ViewControllers
. 我的主要目的是为我的所有ViewControllers
提供一个背景。 Every ViewController
I have has a clear background. 我拥有的每个ViewController
都有清晰的背景。
To do this, I have made one UIViewController
(called backgroundViewController
) that will act as the subview
to all my other ViewControllers
. 为此,我制作了一个UIViewController
(称为backgroundViewController
),它将作为我所有其他ViewControllers
的subview
。 It has one UIImageView
which displays this particular background. 它有一个UIImageView
显示此特定背景。 I will then add this backgroundViewController
as a subview
of all my other ViewControllers
. 然后,我将这个backgroundViewController
添加为所有其他ViewControllers
的subview
。
The problem is - this imageView
won't show as a subview! 问题是-此imageView
不会显示为子视图!
This is how I display the 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];
}
}
As you can see, backgroundView
changes each time the music player skips song. 如您所见,每次音乐播放器跳过歌曲时, backgroundView
更改。
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 . 为了测试backgroundViewController
确实显示为子视图,我添加了另一个imageView并将其图像更改为Interface Builder中的静态.png, 并且正确显示为subview 。
This is how I make it a subview for other ViewControllers
: 这就是我如何使其成为其他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. 我想要的是被称为backgroundView
的UIImageView
在被称为子视图时显示。
I have tested that backgroundView
does change according to what song is playing, and it works correctly. 我已经测试了backgroundView
确实会根据播放的歌曲而变化,并且可以正常工作。
What am I doing wrong? 我究竟做错了什么? backgroundView
refuses to show up as a subview
?! backgroundView
拒绝显示为subview
? I've searched a ton about adding ViewControllers
as subviews
but I can't find a similar problem. 我搜索了大量关于将ViewControllers
添加为subviews
但找不到类似的问题。
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'. 我需要阅读有关“自定义容器”的更多信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.