我有一个“基于导航的应用程序”,它还需要始终在屏幕底部显示一个视图。添加 UINavigationController 的视图后,我将这个新视图添加到 UIWindow 中:
// In my delegate's applicationDidFinishLaunching method
[window addSubview:navigationController.view];
[window insertSubview:disclaimerController.view aboveSubview:navigationController.view];
[window makeKeyAndVisible];
除了旋转之外,这工作正常。我添加的第二个视图无法正确旋转。它不会改变位置,并且不会调用视图控制器的旋转方法。
显然,我的处理方式是错误的。我的问题是,如何在屏幕上获得不属于导航控制器视图一部分的第二个视图?谢谢。
如果将另一个视图控制器的视图作为子视图添加到应用程序的活动窗口,则必须同步其center
, bounds
and transform
属性与window.rootViewController.view
。还要确保您的顶视图已添加after窗口的正确初始化,以及after它的根子视图已作为子视图添加到其中。
我想这可以通过多种方式完成,我已经通过使用键值观察.
这里的topViewController
是一个实例UIViewController
子类,它代表视图控制器UIView
它应该位于应用程序窗口的顶部
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];
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)