Lock screen orientation for a single controller when there's a UITabBar in IOS 5 & 6 - xcode

Following are the main poins I have.
I'm new to IOS development
I need to support IOS 5 and IOS 6 in my IOS app
I have a UITabBarController in AppDeligate file as follows.
UIViewController *viewController1 = [[[Grid1ViewController alloc] initWithNibName:#"Grid1ViewController" bundle:nil] autorelease];
UIViewController *viewController2 = [[[VideoListViewController alloc] initWithNibName:#"VideoListViewController" bundle:nil] autorelease];
UIViewController *viewController3 = [[[MusicViewController alloc] initWithNibName:#"MusicViewController" bundle:nil] autorelease];
UIViewController *viewController4 = [[[GalleryViewController alloc] initWithNibName:#"GalleryViewController" bundle:nil]autorelease];
UINavigationController *myNav1=[[UINavigationController alloc] initWithRootViewController:viewController1];
UINavigationController *myNav2=[[UINavigationController alloc] initWithRootViewController:viewController2];
UINavigationController *myNav3=[[UINavigationController alloc] initWithRootViewController:viewController3];
UINavigationController *myNav4=[[UINavigationController alloc] initWithRootViewController:viewController4];
UIImage *navBackgroundImg = [UIImage imageNamed:#"aa.png"];
[myNav1.navigationBar setBackgroundImage:navBackgroundImg forBarMetrics:UIBarMetricsDefault];
[myNav2.navigationBar setBackgroundImage:navBackgroundImg forBarMetrics:UIBarMetricsDefault];
[myNav3.navigationBar setBackgroundImage:navBackgroundImg forBarMetrics:UIBarMetricsDefault];
[myNav4.navigationBar setBackgroundImage:navBackgroundImg forBarMetrics:UIBarMetricsDefault];
self.tabBarController = [[[UITabBarController alloc] init] autorelease];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:myNav1, myNav2, myNav3,myNav4,nil];
4.I want to lock the rotation of every controller which is directly connected to tabBarController( ex - Grid1ViewController). But I need to rotate the some controller which are used in the app. Please refer the following drawing. A,B,C,D and E are controllers.
I tried implementing following method in controllers and AppDeligate file. But didn't work. When rotation are allowed the whole app start to rotate.
(BOOL)shouldAutorotateToInterfaceOrientation(UIInterfaceOrientation)toInterfaceOrientation{
NSLog(#"5 rotation");
return (toInterfaceOrientation == UIInterfaceOrientationPortrait);
}
// For IOS 6
-(BOOL)shouldAutorotate {
NSLog(#"shouldAutorotate");
return YES;
}
(NSUInteger)supportedInterfaceOrientations {
NSLog(#"6 rotation");
return UIInterfaceOrientationMaskPortrait;
}
How can I achieve this? Please help. Thank you in advance.

Related

UISplitViewControllerDisplayModePrimaryOverlay causes "Unbalanced calls to begin/end appearance transitions"

In iOS 8, setting the preferredDisplayMode on a UISplitViewController to PrimaryOverlay is generating the following warning:
"Unbalanced calls to begin/end appearance transitions for UINavigationController"
There is no problem if I set preferredDisplayMode to AllVisible or don't set it at all. Problem occurs for all iPads and iPhones in the simulator that I've tried. Problem occurs whether the app starts up in portrait or landscape.
Here's some very simple code:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
UITableViewController *tableViewController = [[UITableViewController alloc] init];
UIViewController *viewController = [[UIViewController alloc] init];
UINavigationController *masterNavController = [[UINavigationController alloc] initWithRootViewController:tableViewController];
UINavigationController *detailNavController = [[UINavigationController alloc] initWithRootViewController:viewController];
UISplitViewController *svc = [[UISplitViewController alloc] init];
[svc addChildViewController:masterNavController];
[svc addChildViewController:detailNavController];
//svc.preferredDisplayMode = UISplitViewControllerDisplayModeAllVisible;
svc.preferredDisplayMode = UISplitViewControllerDisplayModePrimaryOverlay;
self.window.rootViewController = svc;
[self.window makeKeyAndVisible];
return YES;
}
Wrap your display code in dispatch_async. Otherwise iOS seems to get confused with other animations running at the same time.
dispatch_async(dispatch_get_main_queue(), ^{
svc.preferredDisplayMode = UISplitViewControllerDisplayModePrimaryOverlay;
});
or
dispatch_async(dispatch_get_main_queue()) {
svc.preferredDisplayMode = .PrimaryOverlay
}

How to present a semi-transparent (half-cut) viewcontroller in iOS 8

in iOS 7 there is no problem for this method:
_rootViewController.modalPresentationStyle = UIModalPresentationCurrentContext;
[_rootViewController presentViewController:self animated:NO completion:nil];
But in iOS 8 it did nothing.How to solve it? Is it a Bug for iOS 8?
My answer is more simple, below code. This works in iOS8 (XCode6 GM seed).
HogeViewController *vc = [[HogeViewController alloc] init];
vc.modalPresentationStyle = UIModalPresentationOverFullScreen;
[self presentViewController:vc animated:NO completion:nil];
Note this workaround was needed on xcode6_beta7. The lastest xcode6
has the UIModalPresentationOver* styles fixed. So, I'm just assigning them to myModalViewController.modalPresentationStyle and now it works ok.
Finally made it work in iOS 8 after reading the UIPresentationController help and this post
appDelegate.window.rootViewController.modalPresentationStyle = UIModalPresentationCurrentContext;
MyModalController *myModalController = [[MyModalController alloc] initWithNibName:#"MyModalController" bundle:nil];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:myModalController];
navController.modalPresentationStyle = UIModalPresentationCustom;
navController.transitioningDelegate = myModalController;
[self.navigationController presentViewController:navController animated:YES completion:nil];
You can make the modal view controller inherit from UIViewControllerTransitioningDelegate
#interface MyModalController : UIViewController <UIViewControllerTransitioningDelegate>
and override presentationControllerForPresentedViewController:...
-(UIPresentationController *)presentationControllerForPresentedViewController:(UIViewController *)presented presentingViewController:(UIViewController *)presenting sourceViewController:(UIViewController *)source
{
if (presented == self) {
return [[TransparentPresentationController alloc] initWithPresentedViewController:presented presentingViewController:presenting];
} else {
return nil;
}
}
returning an instance of TransparentPresentationController which inherits from UIPresentationController
#interface TransparentPresentationController : UIPresentationController
and overrides shouldRemovePresentersView
- (BOOL) shouldRemovePresentersView {
return NO;
}

How to Load View Controllers At the Beginning of the App?

I have a Tab View Controller as my root controller in an App which consists of 3 tabs let's call them View A, View B, View C.
I want to load these tabs as soon as my app starts, I think it can be done within didFinishLaunchingWithOptions function but I'm not exactly sure, does anyone know how to do it?
Thanks
For these kinds of purposes I used this approach:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UIWindow *window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window = window;
UITabBarController *tabBarController = [[UITabBarController alloc] init];
rootView = [[SGMenuViewController alloc] init];
UIViewController *tab1VC = [[UIViewController alloc] init];
UIViewController *tab2VC = [[UIViewController alloc] init];
UIViewController *tab3VC = [[UIViewController alloc] init];
UIViewController *tab4VC = [[UIViewController alloc] init];
navController = [[UINavigationController alloc] initWithRootViewController:rootView];
UINavigationController *secondNavController = [[UINavigationController alloc] initWithRootViewController:tab3VC];
NSArray* controllers = [NSArray arrayWithObjects:navController, tab2VC, secondNavController, tab4VC, nil];
tabBarController.viewControllers = controllers;
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.rootViewController = tabBarController;
[self.window makeKeyAndVisible];
return YES;
}
You basically create a UITabBarController and you put all your view there. You create a UINavigationController if you need to push/pop views in the selected tab. In the example above only the 1st and the 3rd tab have UINavigationController and so I can use self.navigationController pushViewController: on them.

Resize the UIViewController View

I am new in iPhone/Ipad Application development and Just learning the UIViewController, UIView etc with Xcode and while using uiviewcontroller want to resize but my tricks logic are not working. please let me know if you have any Idea with this.
Thanks
i think your are looking like this.
(IBAction)buttonPressed:(id)sender
{
menuView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"bgground.jpg"]];// i used 1 view name is menuView
UIViewController *menu =[[UIViewController alloc] init];menu.view = menuView;
[menu setModalTransitionStyle:UIModalTransitionStylePartialCurl];
[self presentModalViewController:menu animated:YES];
[menu release];
}
After resize try this code
UIViewController* view = [[UIViewController alloc] init];
[self presentModalViewController: view animated: NO];
[self dismissModalViewControllerAnimated: NO];
[view release];

Init a UInavigationbar

I am having trouble in making a tableview with navigation bar or controller on top.
I have this piece of code,
- (void)viewDidLoad {
[super viewDidLoad];
UINavigationController *addNavCon = [[UINavigationController alloc]initWithNibName:#"Welcome" bundle:nil];
self.navigationItem.rightBarButtonItem = self.addButtonItem;
self.navigationItem.leftBarButtonItem = self.editButtonItem;
[self createEditableCopyOfDatabaseIfNeeded];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(applicationWillTerminate:) name:UIApplicationWillTerminateNotification object:nil];
NSString *documentDirectory = [self applicationDocumentsDirectory];
NSString *path = [documentDirectory stringByAppendingPathComponent:#"notebook.plist"];
NSMutableArray *tmpArray = [[NSMutableArray alloc] initWithContentsOfFile:path];
self.Notes = tmpArray;
[tmpArray release];
}
However, the navigation bar never show up, while the table is doing fine. May i know what's the problem with the code?
Many thanks
You have created an instance of UINavigationController but never added it to anything. You need to add it to the current hierarchy or it will not appear.
If you wish to add the navController to the entire app, then you should do this in the App Delegate by
- (void)applicationDidFinishLaunching:(UIApplication *)application
{
UIViewController *rootController = [[MyRootViewController alloc] init];
UINavigationController *navigationController = [[UINavigationController alloc]
initWithRootViewController:rootController];
[rootController release];
window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[window addSubview:navigationController.view];
[window makeKeyAndVisible];
}

Resources