Here are two suggestions how to solve a horizontal view, that is, a view that differs from the vertical.
Either make it a modal with a risk that it takes ”time” until it appears, the transition can be a bit slow.
.h
@interface XXXX : UIViewController
{
BOOL isShowingLandscapeController;
}
@property (nonatomic) BOOL isShowingLandscapeController;
.m
- (void)viewDidLoad
{
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:) name:UIDeviceOrientationDidChangeNotification object:nil];
}
-
(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) orientation {
return (orientation == UIInterfaceOrientationPortrait);
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
[[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];
[super dealloc];
}
- (void)orientationChanged:(NSNotification
*)notification
{
[self performSelector:@selector(updateLandscapeView) withObject:nil afterDelay:0];
}
- (void)updateLandscapeView
{
UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;
if (UIDeviceOrientationIsLandscape(deviceOrientation) && !isShowingLandscapeController)
{
UIViewController viewController = [[UIViewController alloc] initWithNibName:nil bundle:nil];
[self presentModalViewController:viewController animated:YES];
[[UIApplication sharedApplication] setStatusBarOrientation: deviceOrientation];
isShowingLandscapeController = YES;
[viewController release];
} else if (UIDeviceOrientationIsPortrait(deviceOrientation) && isShowingLandscapeController) {
[self dismissModalViewControllerAnimated:YES];
isShowingLandscapeController = NO;
}
}
You can also choose to do in a different way and it is to use dual views, a tip is to only load what is visible but the code is not included in the example.
.h
@interface XXXX : UIViewController
{
UIViewController *viewPortrait;
UIViewController *viewLandscape;
}
@property (nonatomic,assign) UIViewController *viewPortrait;
@property (nonatomic,assign) UIViewController *viewLandscape;
.m
- (void)loadView {
viewPortrait = [[UIViewController alloc] initWithNibName:nil bundle:nil];
viewLandscape = [[UIViewController alloc] initWithNibName:nil bundle:nil];
viewPortrait.hidden = NO;
viewLandscape.hidden = YES;
}
- (void)viewDidLoad
{
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:) name:UIDeviceOrientationDidChangeNotification object:nil];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) orientation {
return (orientation == UIInterfaceOrientationPortrait);
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
[[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];
[super dealloc];
}
- (void)orientationChanged:(NSNotification *)notification
{
[self performSelector:@selector(updateLandscapeView) withObject:nil afterDelay:0];
}
- (void)updateLandscapeView
{
UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;
if (UIDeviceOrientationIsLandscape(deviceOrientation) )
{
viewPortrait.hidden = YES;
viewLandscape.hidden = NO;
} else if (UIDeviceOrientationIsPortrait(deviceOrientation) ) {
viewPortrait.hidden = NO;
viewLandscape.hidden = YES;
}
}
Comments
No comments yet.