使用 insertSubview 时出现方向问题 [英] Orientation Problem while using insertSubview
问题描述
我在使用以下代码在拆分视图之上显示视图时遇到方向问题.
I get an orientation problem while using the following to code to display a view on top of a split view.
[window addSubview:aSplitViewController.view];
[window insertSubview:aViewController.view aboveSubview:aSplitViewController.view];
普通视图有几个按钮和标签.
the plain view has a couple of buttons and labels.
所以我面临的问题是第一个视图以横向模式打开,但视图上的标签和按钮处于纵向模式.
So the problem I am facing is that the first view opens in landscape mode but the labels and buttons on the view are in portrait mode.
更新:这是一些代码,所以如果有人想查看更多详细信息...
UPDATE: Here is some code so if anyone wants to see more details...
在我的应用委托中
- (void) makeSplitViewController {
NSMutableArray *controllers = [NSMutableArray arrayWithArray:tabBarController.viewControllers];
// First tabbbar item
// detail view
detailViewController = [[DetailViewController alloc] initWithNibName:@"DetailView" bundle:nil];
UINavigationController *navDetailView = [[[UINavigationController alloc] initWithRootViewController:detailViewController] autorelease];
navDetailView.hidesBottomBarWhenPushed = YES;
// root view
rootViewController = [[RootViewController alloc] initWithStyle:UITableViewStylePlain];
rootViewController.detailViewController = detailViewController;
rootViewController.navigationItem.title = @"List";
UINavigationController *navRootView = [[[UINavigationController alloc] initWithRootViewController:rootViewController] autorelease];
navRootView.hidesBottomBarWhenPushed = YES;
navRootView.navigationBar.barStyle = UIBarStyleBlackTranslucent;
splitViewController = [[UISplitViewController alloc] init];
splitViewController.tabBarItem.title = @"Face Sheet";
splitViewController.tabBarItem.image = [UIImage imageNamed:@"gear1.png"];
splitViewController.navigationItem.title = @"Face Sheet";
splitViewController.viewControllers = [NSArray arrayWithObjects:navRootView, navDetailView, nil];
splitViewController.delegate = detailViewController;
splitViewController.hidesBottomBarWhenPushed = YES;
[controllers addObject:splitViewController];
// Second tabbbar item
scoreViewController = [[ScoreCardViewController alloc] initWithNibName:@"TableViewController" bundle:nil];
scoreViewController.tabBarItem.title = @"Score Card";
scoreViewController.tabBarItem.image = [UIImage imageNamed:@"gear1.png"];
scoreViewController.navigationItem.title = @"Score Card";
[controllers addObject:scoreViewController];
tabBarController.viewControllers = controllers;
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
// Create tabbar
tabBarController = [[UITabBarController alloc] init];
//tabBarController.delegate = self;
// Set window
[window addSubview:splashController.view];
[window insertSubview:tabBarController.view belowSubview:splashController.view];
[self.window makeKeyAndVisible];
application.statusBarOrientation = UIInterfaceOrientationLandscapeRight;
return YES;
}
这是我的 SplashScreenView 中的代码
and here is the code in my SplashScreenView
- (IBAction) proceedButtonClick:(id)sender
{
// Initialize loginpopview
PhysicianLoginViewController *loginViewController = [[PhysicianLoginViewController alloc] init];
popOverController = [[UIPopoverController alloc] initWithContentViewController:loginViewController];
popOverController.popoverContentSize = CGSizeMake(350, 200);
popOverController.delegate = self;
// Set a notification to dismiss it later
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loginViewControllerDone:) name:@"loginViewControllerDone" object:popOverController.contentViewController];
// Present popover
if ([popOverController isPopoverVisible])
{
[popOverController dismissPopoverAnimated:YES];
}
else
{
[popOverController presentPopoverFromRect:CGRectMake(485, 600, 100, 100) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionDown animated:YES];
}
}
// Dismiss popview controller and setup the tabbar
- (void)loginViewControllerDone:(NSNotification *)notification{
[[NSNotificationCenter defaultCenter] removeObserver:self];
// Button in content view controller was tapped, dismiss popover...
[self.popOverController dismissPopoverAnimated:YES];
// remove subview
[self.view removeFromSuperview];
// set tabbar
i3EAppDelegate *appDelegate = (i3EAppDelegate *) [[UIApplication sharedApplication]delegate];
[appDelegate makeSplitViewController];
}
如果有人能指出我哪里出错了,那就太好了.我已经被这个问题困扰了好几天,我已经尝试了我想到的一切......
It would be great if someone could point out where I am going wrong. I have been stuck with this problem for quite a few days and I have tried everything that comes to my mind...
推荐答案
UIWindow
有一个子视图,它用于旋转并将其他视图放入其中.您需要将自己插入根视图(或更低的视图),而不是窗口.看-[UIWindow rootViewController]
.
UIWindow
has a subview that it uses for rotations and puts other views inside of that. You need to insert yourself into the root view (or something lower), not the window. Look at -[UIWindow rootViewController]
.
UIView *rootView = [[[self window] rootViewController] view];
[rootView addSubview:view];
只要您使用带有根视图控制器的东西,这就会起作用.只要 rootViewController 不是 nil
,这就会起作用.如果您正在开发一个原始的基于视图"的应用程序,那么通常最好选择另一个视图并将您的视图添加为其兄弟视图,而不是挖掘未记录的层次结构:
This will work as long as you're using something with a root view controller. This will work as long as rootViewController isn't nil
. If you're doing a raw "View Based" application, then it's usually best to pick another view and add your view as its sibling rather than digging through the undocumented hierarchy:
UIView *sibling = ... (some other view)
[[sibling superview] addSubview:view];
这篇关于使用 insertSubview 时出现方向问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!