使用insertSubview时的方向问题

使用insertSubview时的方向问题

本文介绍了使用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时的方向问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 09:41