yashwanth77
yashwanth77

Reputation: 1870

How to open a view controller in landscape mode?

I have a table view and when the cell is tapped in table, I'm pushing another view controller as follows:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

 UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone" bundle:nil];
 PriceChart *vc = [storyboard instantiateViewControllerWithIdentifier:@"pricechart"];
 [self.navigationController pushViewController:vc animated:YES];

 }

Now My question is: the view controller I'm going to show should be in land scape mode but it is in portrait mode.

Another question is how to open different view controllers when different cell is tapped. I tried it using indexpath.row but is there any other way using storyboard?

Upvotes: 3

Views: 1042

Answers (5)

Rashad
Rashad

Reputation: 11197

shouldAutorotate, supportedInterfaceOrientations, preferredInterfaceOrientationForPresentation

The above methods don't get called of a UIViewController if they are inside any UINavigationController. If these methods are declared inside UINavigationController then they will get called.

For solving this make a class lets call it OrientationEnabledNavigation, it is a subclass of UINavigationController. Then implemented shouldAutorotate, supportedInterfaceOrientations, preferredInterfaceOrientationForPresentation methods inside OrientationEnabledNavigation.

OrientationEnabledNavigation.h

#import <UIKit/UIKit.h>

@interface OrientationEnabledNavigation : UINavigationController

@end

OrientationEnabledNavigation.m

#import "OrientationEnabledNavigation.h"

@interface OrientationEnabledNavigation ()

@end

@implementation OrientationEnabledNavigation

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (BOOL)shouldAutorotate
{
    return [self.topViewController shouldAutorotate];
//    return NO;
}

- (NSUInteger)supportedInterfaceOrientations
{
    return [self.topViewController supportedInterfaceOrientations];
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return [self.topViewController preferredInterfaceOrientationForPresentation];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

Then if you want to use navigation controller in your project then use OrientationEnabledNavigation. After that if you implement shouldAutorotate, supportedInterfaceOrientations, preferredInterfaceOrientationForPresentation these method inside your viewcontroller then they will get called.

The in you viewcontroller implement these:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

-(BOOL)shouldAutorotate
{
    return NO;
}

-(NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

Another way is that add some code beginning of your app delegate:

For UITabBarController

@implementation UITabBarController (AutoRotationForwarding)

-(BOOL)shouldAutorotate
{
    if ([self.selectedViewController respondsToSelector:@selector(shouldAutorotate)]) {
        return [self.selectedViewController shouldAutorotate];
    }
    else {
        return YES;
    }
}

- (NSUInteger)supportedInterfaceOrientations {
    if ([self.selectedViewController respondsToSelector:@selector(supportedInterfaceOrientations)]) {
        return [self.selectedViewController supportedInterfaceOrientations];
    }
    else {
        return UIInterfaceOrientationMaskAll;
    }
}

@end

For UINavigationController

@implementation UINavigationController (AutoRotationForwarding)

-(BOOL)shouldAutorotate
{
    if ([self.topViewController respondsToSelector:@selector(shouldAutorotate)]) {
        return [self.topViewController shouldAutorotate];
    }
    else {
        return YES;
    }
}

-(NSUInteger) supportedInterfaceOrientations {
    if([self.topViewController respondsToSelector:@selector(supportedInterfaceOrientations)])
    {
        return [self.topViewController supportedInterfaceOrientations];
    }
    return UIInterfaceOrientationMaskPortrait;
}

@end

Hope this helps.. :)

Upvotes: 1

E. Rivera
E. Rivera

Reputation: 10938

You can force an orientation refresh as explained here and the other answers in that question.

As for the second question, just populate a UITableViewController with either static cells or dynamic prototype cells. Then control+drag from the cell to another view controller in your Storyboard. You'll have the same Push/Modal/Custom actions that show up for buttons.

Upvotes: 0

Rajesh Choudhary
Rajesh Choudhary

Reputation: 1340

Try This.. This demo has solved my problem...

https://github.com/alloy/ForceOrientationTest

Upvotes: 0

nmh
nmh

Reputation: 2503

In class which is in landscape mode:

- (NSUInteger)supportedInterfaceOrientations
{
// return orientation that you want
//return UIInterfaceOrientationMaskPortrait|UIInterfaceOrientationMaskPortraitUpsideDown;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
return YES;
//return UIInterfaceOrientationIsPortrait(toInterfaceOrientation);
}

If you use UINavigationController, you should inherit your navigationController from UINavigationController and implement

- (BOOL)shouldAutorotate
{
return YES;
}

- (NSUInteger)supportedInterfaceOrientations
{
return [[self.viewControllers lastObject] supportedInterfaceOrientations];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
return [[self.viewControllers lastObject] shouldAutorotateToInterfaceOrientation:toInterfaceOrientation];
}

Upvotes: 0

Jay Gajjar
Jay Gajjar

Reputation: 2741

This is how you can force to landscape

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

-(BOOL)shouldAutorotate
{
    return NO;
}

-(NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

Upvotes: 0

Related Questions