Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
467 views
in Technique[技术] by (71.8m points)

ipad - 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...

In my App Delegate

- (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;

}

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...

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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];

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];

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...