在没有后台闪烁的情况下在多个UIViewControllers之间共享UIView [英] Sharing a UIView across multiple UIViewControllers without background flashing

查看:86
本文介绍了在没有后台闪烁的情况下在多个UIViewControllers之间共享UIView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在父级和子级UIViewController之间共享一个UIView,当它作为子视图添加到子视图时没有任何明显的视觉故障?

Is there way to share a UIView between a parent and child UIViewController without any noticeable visual glitches when it's added to the child's view as a subview?

我有一个UIViewController及其相应的UIView,被视为标头,将在父和子UIViewController之间共享。 (图像类似于股票代码,将出现在屏幕上的某个位置,在应用程序的所有屏幕上)

I have a UIViewController and its corresponding UIView, considered as a 'masthead', that will be shared between a parent and child UIViewController. (image something like a stock ticker that will be present somewhere on the screen, across all screens in the app)

当创建子UIViewController并将其推送到视图层次结构时(我在'UINavigationController'中使用它们),我看到的是在将标头视图添加为子视图之前它的占位符背景区域达到峰值。

When the child UIViewController is created and pushed on to view hierarchy (I'm using them with 'UINavigationController'), what I see is its placeholder background area peaking through before the masthead view is added as a subview.

我想过为每个屏幕创建唯一的标头视图,但大多数应用的视图将共享此标头。因此管理所有这些内容的内容变化似乎很复杂,我试图通过拥有1个实例来采用更简单的路径。

I thought about creating unique masthead view for each screen but most of the app's views will share this masthead. Thus managing content changes across all of them seemed complicated and I'm trying to take the simpler route by having 1 instance.

AppDelegate工作:

AppDelegate work:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Other prep work, including setup of self.window
    UIViewController *vc = [[ParentViewController alloc] initWithNibName:nil 
                                bundle:[NSBundle mainBundle]];

    UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:vc];        

    self.window.rootViewController = navController;
}

Parent UIViewController实现:

The Parent UIViewController implementation:

@interface ParentViewController : UIViewController {}

@implementation ParentViewController()
- (void)viewDidLoad
{
    // The shared controller and its view has already been created and initialized.

    // Adding the masthead to my view
    [self.view addSubview:self.mastheadController.view];
    [super viewDidLoad];
}
- (void)showChildController
{
    DetailViewController *detailController = [[DetailViewController alloc] initWithNibName:nil
                           bundle:[NSBundle mainBundle]                                                                                 
                           withMastheadController:self.mastheadController];

    [self.navigationController pushViewController:detailController animated:YES];
    detailController = nil;
}

这是Child UIViewController实现:

Here's Child UIViewController implementation:

@interface DetailViewController : UIViewController {}

@implementation DetailViewController()
- (void)willMoveToParentViewController:(UIViewController *)parent
{
    // Since this method is invoked before our viewDidLoad and the 
    // parent's viewWillDisappear, remove shared view from parent view 
    // stack.
    [self.mastheadController.view removeFromSuperview];
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Adding the shared masthead controller view to our view
    [self.view addSubview:self.mastheadController.view];                         
}

我使用willMoveToParentViewController的原因是因为我认为如果我等到'viewWillDisappear'被调用为ParentViewController,它没有足够的时间将mastheadView插入到子视图层次结构中而没有任何明显的效果。

The reason I'm using willMoveToParentViewController is because I thought that if I wait until 'viewWillDisappear' gets called for the ParentViewController, it was not enough time for the mastheadView to get inserted into the child view hierarchy without any noticeable effects.

这是我对视图出现/消失事件发生顺序的理解:

This is my understanding about the order in which the view appear/disappear events happen:

child:willMoveToParentViewController (parent will be null upon dismiss)
child:viewDidLoad
parent:viewWillDisappear
child:viewWillAppear

我们非常感谢任何帮助。

Any help would be greatly appreciated.

推荐答案

我会以不同的方式处理为什么不将视图添加到 navController 的视图中,然后在您不想再看到它时将其删除?

I would approach this differently. Why not add the view to navController's view, and then remove it when you don't want to see it any more?

这篇关于在没有后台闪烁的情况下在多个UIViewControllers之间共享UIView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆