Use a UIViewController and two XIB processing directions to change on the iPad

I want to use a UIViewController and two XIBs to handle the orientation change of an iPad application, that is, MenuView and MenuViewLandscape.

So, in MenuViewController In the willRotateToInterfaceOrientation method, how can I change the XIB without using another controller for landscape mode?

I use the following code:

if( toInterfaceOrientation != UIInterfaceOrientationPortrait ){
MenuViewController *landscape = [[MenuViewController alloc]
initWithNibName: @"MenuViewLandscape"
bundle:nil
];
[self setView:landscape.view];
}
else {
MenuViewController * potrait = [[MenuViewController alloc]
initWithNibName: @"MenuView"
bundle:nil
];
[self setView:potrait.view];
}

However, when I go to view XIB horizontally, the landscape view controls are not rotated properly.

I don’t know any strange side effects of this implementation, but try something like this and see if it works for you:

-(void)willRotateToInterfaceOrientation: (UIInterfaceOrientation)orientation duration:(NSTimeInterval)duration {
i f (UIInterfaceOrientationIsPortrait(orientation)) {
[[NSBundle mainBundle] loadNibNamed:@"MenuView" owner:self options:nil];
if (orientation == UIInterfaceOrientationPortraitUpsideDown) {
self.view .transform = CGAffineTransformMakeRotation(M_PI);
}
} else if (UIInterfaceOrientationIsLandscape(orientation)){
[[NSBundle mainBundle] loadNibNamed:@"MenuViewLandscape" owner:self options:nil];
if (orientation == UIInterfaceOrientationLandscapeLeft) {
self.view.transform = CGAffineTransformMakeRotation(M_PI + M_PI_2);
} else {
self.view.transform = CGAffineTransformMakeRotation(M_PI_2) ;
}
}
}

This assumes that the file owner in your MenuView and MenuViewLandscape XIB is set to MenuViewController, and the view exit is also set in both XIBs When using loadNibNamed, all outlets should be reconnected correctly during rotation.

If you are building for iOS 4, you can also use the following command to replace the loadNibNamed line:

< /p>

UINib *nib = [UINib nibWithNibName:@"MenuView" bundle:nil];
UIView *portraitView = [[nib instantiateWithOwner:self options:nil] objectAtIndex:0];
self.view = portraitView;

and

UINib *nib = [UINib nibWithNibName:@"MenuViewLandscape" bundle:nil];
UIView *landscapeView = [[nib instantiateWithOwner:self options:nil] objectAtIndex:0];
self.view = landscapeView;

These assume that the UIView you want to display is immediately after the owner of the file in the XIB and the first responder proxy object.

Then, you just need to make sure that the view is rotated correctly in the interface orientation. For all views that are not the default portrait orientation, rotate by setting the transform property of the view and using CGAffineTransformMakeRotation() with the appropriate value shown in the above example.

Single rotation may solve your problem without additional loading of NIB. However, loading a brand new instance of MenuViewController and setting its view to the existing MenuViewController view may cause some weirdness in life cycle and loop events So you might be safer to try the above example. They can also save the trouble of creating a new MenuViewController instance when you only need to view the view from it.

Hope this helps!

Justin

I want to use a UIViewController and two XIBs to handle the orientation change of an iPad application, that is, MenuView and MenuViewLandscape.

So, in the willRotateToInterfaceOrientation method of MenuViewController, how to change XIB without using another controller for landscape mode?

I use the following code:

if( toInterfaceOrientation != UIInterfaceOrientationPortrait ){
MenuViewController *landscape = [[MenuViewController alloc]
initWithNibName: @"MenuViewLandscape"
bundle:nil
];
[self setView:landscape.view];
}
else {
MenuViewController * potrait = [[MenuViewController alloc]
initWithNibName: @"MenuView"
bundle:nil
];
[self setView:potrait.view];
}

However, when I go to view the XIB horizontally, the landscape view controls are not rotated correctly.

I don't know what strange side effects this implementation has , But try something like this and see if it works for you:

-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)orientation duration:(NSTimeInterval)duration {< br /> if (UIInterfaceOrientationIsPortrait(orientation)) {
[[NSBundle mainBundle] loadNibNamed:@"MenuView" owner:self options:nil];
if (orientation == UIInterfaceOrientationPortraitUpsideDown) {
self.view.transform = CGAffineTransformMakeRotation(M_PI);
}
} else if (UIInterfaceOrientationIsLandscape(orientation)){
[[NSBundle mainBundle] loadNibNamed:@"MenuViewLandscape" owner:self options:nil];
if (orientation == UIInterfaceOrientationLandscapeLeft) {
self.view.transform = CGAffineTransformMakeRotation(M_PI + M_PI_2);
} else {
self.view.transform = CGAffineTransformMakeRotation(M_PI_2);
}
}< br />)

This assumes that the file owner in your MenuView and MenuViewLandscape XIB is set to MenuViewController, and the view exit is also set in both XIBs. When using loadNibNamed, all exits should be in Reconnect correctly when rotating.

If you are building for iOS 4, you can also replace the loadNibNamed line with the following command:

UINib *nib = [UINib nibWithNibName:@"MenuView" bundle:nil];
UIView *portraitView = [[nib instantiateWit hOwner:self options:nil] objectAtIndex:0];
self.view = portraitView;

and

UINib *nib = [UINib nibWithNibName:@"MenuViewLandscape" bundle:nil];
UIView *landscapeView = [[nib instantiateWithOwner:self options:nil] objectAtIndex:0];
self.view = landscapeView;

These assume that the UIView you want to display is immediately after the owner of the file in the XIB and the first responder proxy object.

Then, you just need to make sure that the view is rotated correctly in the interface direction. For not All views in the default portrait orientation are rotated by setting the transform attribute of the view and using CGAffineTransformMakeRotation() with the appropriate values ​​shown in the above example.

A separate rotation may solve your problem without the need Additional loading of NIB. However, loading a brand new instance of MenuViewController and setting its view to the existing MenuViewController view may cause some strange behavior in life cycle and loop events, so you may be safer to try the above example. When you They can also save the trouble of creating a new MenuViewController instance when they only need to view the view from it.

Hope this helps!

Justin

WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]
SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 1009 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC

Leave a Comment

Your email address will not be published.