Modal ViewController on top of NavigationController - uiviewcontroller

I'm trying to add a menuViewController on top of my navigationBar and rootView.
menuView = [[MenuViewController alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width*0.4, self.view.frame.size.height)];
UIViewController *rootViewController = [[[[UIApplication sharedApplication] delegate] window] rootViewController];
rootViewController.modalPresentationStyle = UIModalPresentationFullScreen;
[[self navigationController] presentViewController:menuView animated:YES completion:nil];
That is my code, however the view goes black after the animation.
Any ideas?

Try using UIModalPresentationCurrentContext instead of UIModalPresentationFullScreen.

Related

How to pass some variable to parent view controller?

I have a UIViewController its my main view controller. When the app starting that main viewcontroller is loading. Then there is a bar button. When I click on that, another view controller loding like this.
LoginViewController *viewController = [[LoginViewController alloc]
initWithNibName:#"LoginViewController" bundle:nil];
viewController.mainViewController = self ;
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:viewController];
[navController setModalPresentationStyle:UIModalPresentationFormSheet];
navController.navigationBar.tintColor = [UIColor blackColor];
[self presentModalViewController:navController animated:YES];
In that new view controller I am doing some tasks and when click the close button, the results should be loaded in on the parent view controller.How can I do this. In parent view controller ViewDidAppear also not calling when I just close the top view controller. How can I do this? Please help me.
Thanks
First of all the line of code
[self presentModalViewController:navController animated:YES];
is deprecated and its been a long time, I don't know why are you still using it.
// Use below code instead
[self presentViewController:viewController animated:YES completion:nil];
Also you need to use protocols and delegates to pass any object to parent viewcontroller while you are dismissing the other viewcontroller.
If you can show up some code we might be able to figure out that why your viewDidAppear is not calling.

Open UIViewController modally from tab bar with transparent background and transition style

I've got a custom tab bar that I created using this link: https://github.com/jain-mohit/CustomTabBar. Now, I want to open a view modally after pressing a button. The opened view must be transparent, meaning I can see the view beneath it. The modal view also must a navigation bar so I can put a "Cancel" button to dismiss it. I've tried it like this:
DetailsViewController *detailViewController = [[DetailsViewController alloc] init];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:detailViewController];
nav.modalPresentationStyle = UIModalPresentationCurrentContext;
nav.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
detailViewController.view.backgroundColor = [UIColor greenColor];
detailViewController.view.alpha = 0.5f;
[self presentViewController:nav animated:YES completion:nil];
This method quite works but when opening, the view doesn't transition from bottom to top. And I also got this error:
Presenting view controllers on detached view controllers is discouraged <UINavigationController: 0x8f52280>
So, I'm looking for another way to do this. Hopefully someone can help. This method should work for iOS6, 7 and 8. Also I'm not using storyboard to do this.
Thank You.
Try to use UIWindow, instead of presenting a new ViewController:
UIWindow *w = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
w.windowLevel = UIWindowLevelAlert;
w.autoresizesSubviews = NO;
w.clipsToBounds = YES;
w.hidden = NO;
[w addSubview:detailViewController];
In this case background of your new ViewController will be transparent, but you can't use UINavigationController here...

Cocos2d + UIViewController switch

Its a game with cocos2d 2.1 beta.
I used seperate viewController that comes when pressing button. When I fast switch between these two then some time game hangs...Not crash..fps label works. Something like stopAnimation..
What's wrong with my code? How can I avoid hang? Only when fast switched.
-(void)showNativeView
{
UIViewController *controller = [[UIViewController alloc] init];
AppController *app = (AppController*) [[UIApplication sharedApplication] delegate];
[[app navController] presentModalViewController:controller animated:NO];
[UIView animateWithDuration:1.0
animations:^{controller.view.alpha = 1.0;}];
[controller release];
}
-(void)gotoGameAgain
{
AppController *app = (AppController*) [[UIApplication sharedApplication] delegate];
[app navController].modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[[app navController] dismissModalViewControllerAnimated:YES];
}
Do you really need to initiate the UIViewController when the button is clicked? It may be better if you initiate only once and than use it when the button is clicked. Initiating it evertime you clicked on the button may be creating some performance issues during the fast switch...

Keep a UIButton fixed on screen when pushing and popping UIViewController

Hi i currently have a navigation based app with a navigation controller, within the app numerous views and pushed and popped back displaying different xibs, what I would like to do is display a button fixed on the screen in the bottom right of the screen, which when tapped will take the user to a help xib,
However at the moment the button is being animated and moved into place every time I push a or pop a view, I would like the button not to move and stay on top of the views when they are being pushed and popped.
Wondering if anyone can help me, thanks, Sami.
Create a UIView Class in that create UIButton And create object of that view in all the viewcontrollers,and add it to the self.view.
using this in appdelegate will help u to show button in all the screen.
if u need handle this button, import viewcontrollerAppdelegate.h to the view controller. and use
UIAppDelegate.SettingButton.hidden=True;
or like this any. just try
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
settingButton = [UIButton buttonWithType:UIButtonTypeCustom];
[settingButton addTarget:self action:#selector(settingButtonPressed:)forControlEvents:UIControlEventTouchDown];
UIImage *buttonImage = [UIImage imageNamed:#"icon-lb.png"];
[settingButton setBackgroundImage:buttonImage forState:UIControlStateNormal];
[settingButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
settingButton.frame = CGRectMake(265, 17, 40, 45);
settingButton.hidden=TRUE;
[self.window addSubview:settingButton];
[self.window makeKeyAndVisible];
return YES;
}
- (void)settingButtonPressed:(id) sender {
[[NSNotificationCenter defaultCenter] postNotificationName:#"settingButtonPressed" object:nil];
setting *obj = [[setting alloc] initWithNibName:#"setting" bundle:nil];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:obj];
navController.navigationBar.tintColor=[UIColor blackColor];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
[self.window addSubview:navController.view];
[navController.view setFrame:CGRectMake(0,480,320,480)];
[UIView setAnimationTransition:UIViewAnimationTransitionNone forView:navController.view cache:YES];
[navController.view setFrame:CGRectMake(0,0,320,480)];
[UIView commitAnimations];
}

UITabbarController change items dynamically

I want to edit UITabbarItems during the view life time.
The background is that I have an App with login view and according to whether the user is admin or not, the admin TabbarItem should be visible or not.
I'm using this code to initially create the UITabbarController in the AppDelegate:
// AppDelegate.m
settings = [[settingsViewController alloc] init];
settings.tabBarItem = [[UITabBarItem alloc] initWithTitle:#"Einstellungen" image:[UIImage imageNamed:#"settings.png"] tag:3];
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:readState, friends, settings, nil];
When I try to manipulate the items later from another UIViewController, nothing happens and the UITabbar remains like it was before.
I actually tried two ways I could imagine:
[[self tabBarController] setToolbarItems:[[[self tabBarController] toolbarItems] arrayByAddingObject:admin] animated:NO];
[[self tabBarController] setViewControllers:[[[self tabBarController] viewControllers] arrayByAddingObject:admin] animated:NO];
How can I reach my goal? Thanks in advance, with kind regards, Julian
I figured out a workaround for my problem. I don't really like to import the AppDelegate but it seems as the tabbarController property is not being automatically set for the UIViewControllers of a UITabbarController.
// generate an instance of the needed UIViewController
adminViewController *admin = [[adminViewController alloc] init];
admin.tabBarItem = [[UITabBarItem alloc] initWithTitle:#"Admin" image:[UIImage imageNamed:#"admin.png"] tag:5];
// get the AppDelegate instance
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
// get the »viewControllers« array of the AppDelegates UITabbarController as mutable array
NSMutableArray *viewControllersArray = [NSMutableArray arrayWithArray:[[appDelegate tabBarController] viewControllers]];
// insert the UITabbarItem of the needed UIViewController
[viewControllersArray insertObject:admin atIndex:2];
// Finally tell the app delegates UITabbarController to set the needed viewControllers
[[appDelegate tabBarController] setViewControllers:viewControllersArray animated:NO];