In tabbar application we want to view only some view to landscape and some to portrait in iphone - iphone

In my application there are three tabs in second tab one viewcontroller in that i want to view only in portrait mode but i m not able to view in that format...so any one have right solution then answer here...

add this methods to your second view controler :
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}
- (BOOL)shouldAutorotate
{
return NO;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
return (toInterfaceOrientation == UIInterfaceOrientationPortrait);
}

Related

Support rotation in only one tab on both iOS 5 and 6

My app is a scorekeeper with two tabs. Tab one is where scorekeeping actually takes place, and tab two contains history of past games. Only tab one supports rotation, and when rotating it hides its navigation and tab bars (there's not enough vertical space). I do not want to support rotation at all on the history tab, because the tabbar is hidden in the counter. It is quite jarring to switch tabs and then have the tab bar disappear.
There are several types of games that may be displayed in the first tab bar, all subclasses of "CounterBase". When a new game is started, the first view controller in the tabbar is swapped out. All rotation code is handled in CounterBase, and not in its subclasses.
I have tried numerous things to support rotation, but they have all been flaky. The best result I have gotten so far is to have everything work properly when the app is loaded for the first time, but for rotation to stop the first time a new game is started (swapping the first view controller).
One thing I have found is that supportedInterfaceOrientationsForWindow in the app delegate is no longer called after the first view controller is swapped out.
What is the best way to handle this?
Update I forgot I am using a tab bar on the iPad because it is always hidden. Rotation works just fine there, so now I'm doubly confused. However, the history tab never shows, which may or may not be relevant.
Update 2 I have also tried using a custom navigation controller and implementing shouldAutorotate. Still didn't work.
Update 3 I have discovered that it is not replacing the first tab that is causing the problem, but presenting a modal view in a navigation controller with presentViewController:animated:completion:. I have tried overriding shouldAutorotate in the presented view controller, but it does nothing. Also, I've added the category I'm using on UINavigationController below.
Here is my current implementation:
Tab bar controller category
-(BOOL)shouldAutorotate{
if (self.selectedIndex == 0) // First tab is Counter
return YES;
else
return NO;
}
- (NSUInteger)supportedInterfaceOrientations{
if (self.selectedIndex==0) {
return UIInterfaceOrientationMaskAll;
}
else {
return UIInterfaceOrientationMaskAllButUpsideDown;
}
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
ALog(#"Tab bar selectedIndex: %d", self.selectedIndex);
return self.selectedIndex == 0;
}
CounterBase
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
if (UI_USER_INTERFACE_IDIOM_PAD()){
return YES;
} else if (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown){
return YES;
}
return NO;
}
- (NSUInteger)supportedInterfaceOrientations {
if (UI_USER_INTERFACE_IDIOM_PHONE())
return UIInterfaceOrientationMaskAllButUpsideDown;
else
return UIInterfaceOrientationMaskAll;
}
- (BOOL) shouldAutorotate {
return YES;
}
HistoryBase
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation{
BilliardsBuddyAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
return appDelegate.tabBarController.selectedIndex == 0;
}
-(BOOL)shouldAutorotate{
return NO;
}
-(NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationPortrait;
}
AppDelegate
- (NSUInteger) application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
ALog(#"Tab bar selectedIndex: %d", tabBarController.selectedIndex);
if (tabBarController.selectedIndex == 0 || UI_USER_INTERFACE_IDIOM_PAD()){
return UIInterfaceOrientationMaskAll;
} else {
return UIInterfaceOrientationMaskPortrait;
}
}
#implementation UINavigationController (autoRotate)
-(BOOL)shouldAutorotate {
return YES;
}
- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskAll;
}
#end
Well, after three days I have finally solved this. I was using Mugunth Kumar's great UIKit categories to present an action sheet. Apparently UIActionSheet does not like having a delegate set that is not a UIViewController. (Or maybe even the topmost view controller. I'm not sure.) Switching back to the standard UIActionSheet showFromTabBar fixed this problem.

I want to restrict some of the view controller to landscape in ios6

I'm trying to restrict one view controller which on top of UINavigationController. To do that i've created a UINavigationController subclass and implemented 2 methods
- (BOOL)shouldAutorotate {
return [[self.viewControllers lastObject] shouldAutorotate];}
- (NSUInteger)supportedInterfaceOrientations {
return [[self.viewControllers lastObject] supportedInterfaceOrientations];}
I want the first viewcontroller on top of UINavigationController(which is Root View Controller) should be in portrait mode and the next view controller which i'm pushing from the root view controller should be Landscape mode(ONLY).
So i'm overriding those two methods in both view controllers.
In the root view controller
- (BOOL)shouldAutorotate {
return NO;}
- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskPortrait;}
In the next view controller
- (BOOL)shouldAutorotate {
return YES;}
- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskLandscape;}
Its working fine but not completely. For the first time when I push the view controller its showing in portrait mode(Not restricting to landscape as I expected) and once I rotate the device/simulator and its working fine and restricting to landscape only.
Can anyone help in this?
Try this out.
Call this one in the viewWillAppear will explicitly tell the device to jump to the portrait orientation.
[[UIDevice currentDevice] performSelector:NSSelectorFromString(#"setOrientation:") withObject:(id)UIInterfaceOrientationPortrait];
I don't think this is the right solution. But if you got no other options, you can use this.
Happy Coding :)
U present new controller :
SecondViewController *objSecondViewController = [[SecondViewController alloc]initWithNibName:#"SecondViewController" bundle:nil];
[self.navigationController presentViewController:objSecondViewController animated:NO completion:^{}];
In new controller :
- (BOOL)shouldAutorotate {
return YES;
}
- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskLandscape;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationLandscapeLeft;
}
This worked for me. Just try using this :
1) YourApp-Info.plist
Add one more array for Supported interface orientations
Add your required orientation to this dictionary
Refer below screenshot :
2) Project Target
Select the required orientation from Supported Interface Orientations
Refer below screenshot :
UIViewController have the following function. You can implement this in your view controller where you want to restict portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
if(interfaceOrientation == UIInterfaceOrientationPortrait)
return YES;
else
return NO;
}

Make a UiViewController stay in portrait mode iOS6 VS iOS5

I'm building an app for iOS5 and iOS6. I have this UIViewController inside a UINavigationController and I want it to stay in portrait mode.
The code works for iOS5, but not in iOS6.
// iOS5 rotation
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
if (interfaceOrientation == UIInterfaceOrientationPortrait)
return YES;
else
return NO;
}
// iOS6 rotation
- (BOOL)shouldAutorotate
{
return YES;
}
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}
What's the problem here??? On SO I've found lot's of similar questions, but generally the answers are not working for me.
EDIT:
Maybe I was not precise, but I need a SINGLE view controller (the homepage of my app) to stay in portrait mode and not all the app.
First of all, A lot depends on with which controller is your UIViewController embedded in.
Eg, If its inside UINavigationController, then you might need to subclass that UINavigationController to override orientation methods like this.
subclassed UINavigationController (the top viewcontroller of the hierarchy will take control of the orientation.) needs to be set it as self.window.rootViewController.
- (BOOL)shouldAutorotate
{
return self.topViewController.shouldAutorotate;
}
- (NSUInteger)supportedInterfaceOrientations
{
return self.topViewController.supportedInterfaceOrientations;
}
From iOS 6, it is given that UINavigationController won't ask its UIVIewControllers for orientation support. Hence we would need to subclass it.
MOREOVER
Then, For UIViewControllers, in which you need only PORTRAIT mode, write these functions
- (BOOL)shouldAutorotate
{
return YES;
}
- (NSUInteger)supportedInterfaceOrientations
{
return (UIInterfaceOrientationMaskPortrait);
}
For UIViewControllers, which require LANDSCAPE too, change masking to All.
- (NSUInteger)supportedInterfaceOrientations
{
return (UIInterfaceOrientationMaskAllButUpsideDown);
//OR return (UIInterfaceOrientationMaskAll);
}
Now, if you want to do some changes when Orientation changes, then use this function.
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
}
VERY IMPORTANT
in AppDelegate, write this. THIS IS VERY IMP.
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
return (UIInterfaceOrientationMaskAll);
}
If you want to provide only Portrait mode for all your viewcontrollers, then apply the portait mask. i.e UIInterfaceOrientationMaskPortrait
Otherwise, if you want that some UIViewControllers stay in Portrait while others support all orientations, then apply an ALL Mask. i.e UIInterfaceOrientationMaskAll
Try out this :
-(BOOL)shouldAutorotate
{
return NO;
}
-(NSUInteger)supportedInterfaceOrientations
{
UIInterfaceOrientationMask orientationMask = UIInterfaceOrientationMaskPortrait;
return orientationMask;
}
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
This should work in order to support the portrait mode only.

iOS Rotate View Only One View Controller's View

I have two view controllers in a single project. However, I want one of the view controller's to autorotate, and the other to not.
If I set the master project setting as seen below:
Then, all view controllers autorotate, regardless of the following code in the view controller I do NOT want to autorotate:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if (interfaceOrientation == UIInterfaceOrientationPortrait) {
return YES;
}
return NO;
}
However, if I set the master project settings as seen below, the view controller that I do not want to autorotate does not, but that also means neither can the one that I DO want to.
How must I integrate the master project (plist file) settings with those of the view controllers so that one view controller will auto-rotate while the other will not?
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
was depreciated in iOS 6 so if that's what your project is running, that's why it's not working. What you need to do is implement:
- (NSUInteger)supportedInterfaceOrientations
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
The first one will tell the controller what orientation(s) it is allowed to use, and the second will tell it which one to use first. Note that the first method is only called if the method shouldAutorotate: returns YES.
These are the constants you can use for supportedInterfaceOrientations:
UIInterfaceOrientationMaskPortrait
UIInterfaceOrientationMaskLandscapeLeft
UIInterfaceOrientationMaskLandscapeRight
UIInterfaceOrientationMaskPortraitUpsideDown
UIInterfaceOrientationMaskLandscape
UIInterfaceOrientationMaskAll
UIInterfaceOrientationMaskAllButUpsideDown
Note that these only work on iOS 6.0.
Assume I am using tabbarController & iOS<6.0 try to use the following code solve your issue:
//In First View Controller
//BOOL activeStatus;
-(void)viewWillAppear:(BOOL)animated
{
activeStatus=YES;
}
-(void)viewWillDisappear:(BOOL)animated
{
activeStatus=NO;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if ((interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight) && activeStatus==YES)
{
return YES;
}
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
//In Second View Controller
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}

How To Restrict Changing Into Landscape Iphone?

I do have five view controllers, and i am navigating from first view Controller to end view Controller (fifth view controller)
In middle i do third view i need to do orientation. for landscape and potrait mode.
and from there it need to be potrait mode only.
I added this delegate method for orientation in fouth viewController . but its not effecting
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Overriden to allow any orientation.
//Support orientation for Portrait
return (interfaceOrientation==UIInterfaceOrientationPortraitUpsideDown || interfaceOrientation==UIInterfaceOrientationPortrait );
}
I think your changing orientation in your first view.Open your third view and write this method then change whatever you want...i think you should write this in all of the view expect third view...
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
Now in your third view write your code....
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Overriden to allow any orientation.
//Support orientation for Portrait
return NO;
}
just do.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
if (interfaceOrientation == UIInterfaceOrientationLandscape)
return NO;
else
return YES;
}