简体   繁体   English

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

[英]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 ),它将作为我所有其他ViewControllerssubview 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添加为所有其他ViewControllerssubview

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. 我想要的是被称为backgroundViewUIImageView在被称为子视图时显示。

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.

相关问题 添加到UIView后如何显示UIImageview动画 - How to show UIImageview animation when added to a UIView 子视图上的UIImagePickerController并在UIImageView上显示照片 - UIImagePickerController on Subview and Show Photo on UIImageView 添加到视图的子视图的UIButton无法响应 - UIButton Added to a View's Subview Won't Respond 如何在另一个ViewController中将带有Xib的ViewController显示为SubView - How to show a ViewController with Xib in Another ViewController as a SubView 推送viewController动画时,UINavigationController上的子视图看起来不正确 - Subview on UINavigationController when pushing viewController animated doesn't look right 以编程方式添加的子视图框架不会在滚动视图中正确显示 - Programmatically added subview frames don't show up right in scrollview 具有UIImageView子视图的UIButton以显示动画图像 - UIButton with UIImageView subview to show animated images ios-charts子视图上拖动时,uiscrollview不会滚动 - uiscrollview won't scroll when dragging on ios-charts subview RootViewController在其委托外部访问时不会添加子视图 - RootViewController won't add subview when accessed outside its delegate 将UIImageview添加到另一个子视图UIScrollview后未显示 - UIImageview not showing after being added to another subview which is UIScrollview
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM