Multiple views in a UIWindow

2020-03-05 08:06发布

问题:

I have a "navigation based application" which also needs to have a view always displayed at the bottom of the screen at all times. I added this new view to a UIWindow after adding the UINavigationController's view:

// In my delegate's applicationDidFinishLaunching method
[window addSubview:navigationController.view];
[window insertSubview:disclaimerController.view aboveSubview:navigationController.view];
[window makeKeyAndVisible];

This works fine except for rotation. The second view I added doesn't rotate correctly. It doesn't change position and it's view controller's rotate methods don't get called.

Obviously, I'm going about this the wrong way. My question is, how can I have a second view on screen that's not part of navigation controller's view? Thanks.

回答1:

If you are adding a view on top of another by adding as subview rotation will not work (view won't be notified of rotation). What you can do is register the view with the device to be notified of rotation events, then you can programatically handle rotation (it will not do it for you).



回答2:

If you add a view of another view controller as a subview to the active window of the application you must synchronize its center, bounds and transform properties with window.rootViewController.view. Also be sure your top view is added after correct initialization of the window, and also after its root subview has been added to it as a subview.

I suppose this can be done in multiple ways, I've made it work through use of Key-value observing.

Here the topViewController is instance of a UIViewController subclass and it represents the view controller of the UIView that should be on top of the application window

AppDelegate.m:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [self.window.rootViewController.view addObserver:topViewController forKeyPath:@"transform" options:NSKeyValueObservingOptionNew context:@"rootView"];
    [self.window.rootViewController.view addObserver:topViewController forKeyPath:@"center" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:@"rootView"];
    [self.window.rootViewController.view addObserver:topViewController forKeyPath:@"bounds" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:@"rootView"];
    return YES;
} 

topViewController's .m file:

-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    if([(__bridge_transfer NSString*)context isEqualToString:@"rootView"])
    {
        if([keyPath isEqualToString:@"transform"])
        {
            self.view.transform = [[change objectForKey:NSKeyValueChangeNewKey] CGAffineTransformValue];
        }
        else if ([keyPath isEqualToString:@"center"]) 
        {
            self.view.center = [[change objectForKey:NSKeyValueChangeNewKey] CGPointValue];
        }
        else if ([keyPath isEqualToString:@"bounds"]) 
        {
            self.view.bounds = [[change objectForKey:NSKeyValueChangeNewKey] CGRectValue];
        }
        else 
        {
            [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
        }
    }
    else 
    {
        [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
    }
}


回答3:

Check out my answer here: https://stackoverflow.com/a/4960988/202451

It should bring you closer to doing custom things like that :)