RootViewController's view is not beeing resized with auto layout - iphone

Hi when using autolayout on iOS 6, my "rootView" of my rootViewController doesnt seem to resize properly if at all.
My rootViewController is loaded from a nib and i am adding it to the view-hierarchy like this:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
RootVC *rootVC = [[RootVC alloc] initWithNibName:#"RootVC" bundle:nil];
self.window.rootViewController = rootVC;
return YES;
}
But when i rotate the simulator the view of rootVC doesnt resize. Since you cant set any constraints to the "rootview" in a xib, I also tried this but without any effect:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
RootVC *rootVC = [[RootVC alloc] initWithNibName:#"RootVC" bundle:nil];
self.window.rootViewController = rootVC;
NSArray *vConst = [NSLayoutConstraint constraintsWithVisualFormat:#"V:|[view]|" options:0 metrics:nil views:#{#"view" : rootVC.view}];
NSArray *hConst = [NSLayoutConstraint constraintsWithVisualFormat:#"H:|[view]|" options:0 metrics:nil views:#{#"view" : rootVC.view}];
[self.window addConstraints:vConst];
[self.window addConstraints:hConst];
return YES;
}
When i log-out the frame-size of the rootvc's view, it's always the portrait-format (w:768, h:1024)
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation{
NSLog(#"%f, %f", self.view.frame.size.width, self.view.frame.size.height);
}
could someone please tell me what i'm missing here? i've wasted hours with this problem and got nowhere^^
thx

It seems that a lot of things can go wrong with the autolayout feature iOS 6. Ambiguities in the constraints and failing to disable the translatesAutoresizingMaskIntoConstraints in your view controllers can cause much conflict. So ensure there is no ambiguities in the constraints by calling the autolayoutTrace method in lldb: po [[UIWindow keyWindow] _autolayoutTrace]
If there are ambiguities, you must resolve them.

Related

Objective - C - NavigationBar Title

This is my AppDelegate, i set a title "Pizza", but when i build and play the application, the text don't will appear.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
UITabBarController *tabBarController = [[UITabBarController alloc]init];
ListaPizzeViewController *listPizzeVC = [[ListaPizzeViewController alloc]init];
UINavigationController *navigationControllerPizza = [[UINavigationController alloc]initWithRootViewController:listPizzeVC];
[navigationControllerPizza setTitle:#"Pizza"];
navigationControllerPizza.tabBarItem.image = [UIImage imageNamed:#"Default.png"];
ListIngredientsViewController *listIngredientVC = [[ListIngredientsViewController alloc]init];
UINavigationController *navigationControllerIngradient = [[UINavigationController alloc]initWithRootViewController:listIngredientVC];
[navigationControllerIngradient setTitle:#"Ingredient"];
navigationControllerIngradient.tabBarItem.image = [UIImage imageNamed:#"Default.png"];
[tabBarController setViewControllers:#[navigationControllerPizza, navigationControllerIngradient]];
[self.window setRootViewController:tabBarController];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
Why?
According to Your Question you can set title of ListaPizzeViewController using object of ListaPizzeViewController like Bellow:-
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
UITabBarController *tabBarController = [[UITabBarController alloc]init];
ListaPizzeViewController *listPizzeVC = [[ListaPizzeViewController alloc]init];
UINavigationController *navigationControllerPizza = [[UINavigationController alloc]initWithRootViewController:listPizzeVC];
[listPizzeVC setTitle:#"Pizza"]; // Here it is you can set Title of Object of Viewcontroller so set like thin not [navigationControllerPizza setTitle:#"Pizza"];
navigationControllerPizza.tabBarItem.image = [UIImage imageNamed:#"Default.png"];
ListIngredientsViewController *listIngredientVC = [[ListIngredientsViewController alloc]init];
UINavigationController *navigationControllerIngradient = [[UINavigationController alloc]initWithRootViewController:listIngredientVC];
[navigationControllerIngradient setTitle:#"Ingredient"];
navigationControllerIngradient.tabBarItem.image = [UIImage imageNamed:#"Default.png"];
[tabBarController setViewControllers:#[navigationControllerPizza, navigationControllerIngradient]];
[self.window setRootViewController:tabBarController];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
go into your ListaPizzeViewController, inside the viewDidLoad write
self.title = #"Pizze";
self.navigationItem.title=#"pizza";
simply call this.
Try to set title in viewWillAppear method.
of "listPizzeVC" as
self.title=#"Pizza";
IT is title is ViewController property so use view controller object and title set I write the code under.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: (NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
UITabBarController *tabBarController = [[UITabBarController alloc]init];
ListaPizzeViewController *listPizzeVC = [[ListaPizzeViewController alloc]init];
UINavigationController *navigationControllerPizza = [[UINavigationController alloc]initWithRootViewController:listPizzeVC];
//[navigationControllerPizza setTitle:#"Pizza"];
listPizzeVC.title=#"Pizza";
navigationControllerPizza.tabBarItem.image = [UIImage imageNamed:#"Default.png"];
ListIngredientsViewController *listIngredientVC = [[ListIngredientsViewController alloc]init];
UINavigationController *navigationControllerIngradient = [[UINavigationController alloc]initWithRootViewController:listIngredientVC];
//[navigationControllerIngradient setTitle:#"Ingredient"];
listIngredientVC.title=#"Ingredient";
navigationControllerIngradient.tabBarItem.image = [UIImage imageNamed:#"Default.png"];
[tabBarController setViewControllers:#[navigationControllerPizza, navigationControllerIngradient]];
[self.window setRootViewController:tabBarController];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
I hope it is useful too.
Your code should not be in the appDelegate but in a viewcontroller class that inherits from UIViewController and attach this class to your xib or storyboard screen. One viewcontroller per screen. Add your code to the viewDidLoad or create your own methods. When this doesn't work you might wanna try and post your question again. ;).
I recently started iOS development, it helped me to take little steps and create small project that aimed for learning one thing at a time this way you will learn really fast and efficient without too much frustration. Good luck!

xcode start simulation in specific line, view, or point

It's not a question about code, it's possible? because i have a project with many view and testing is not to fast.
You could set a different view in your root controller at
-applicationDidFinishLaunching:withOptions:
its possible but in some view you will get problem because your some view based on those view which are't loading.
by the way here is code
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
self.YourviewController = [[[self.YourviewController alloc] initWithNibName:#"self.YourviewController" bundle:nil] autorelease];
self.window.rootViewController = self.YourviewController;
[self.window makeKeyAndVisible];
return YES;
}

Ipad presentModalViewController over an ViewController

I have ViewController(Passwordviewcontroller) which I want to show with "presentModalviewController"
I have an AppDeleage:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
self.viewController = [[[ViewController alloc] initWithNibName:#"ViewController" bundle:nil] autorelease];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}
Here is My ViewController from which I want the PasswordviewController to show:
-ViewDidLoad
{
self.passwordView = [[PasswordView alloc]initWithNibName:#"PasswordView" bundle:nil];
[passwordView setModalPresentationStyle:UIModalPresentationFullScreen];
[self presentModalViewController:passwordView animated:YES];
}
I tried everything but its still not working, has somebody an Idea?
Put this in viewDidAppear instead of viewDidLoad. In viewDidLoad your view might be loaded into memory but doesn't have to be on-screen yet. In viewDidAppear on the other hand your view is ready to be shown an properly inserted in the window in such a way that you may show modal views.
Edit: Changed viewWillAppear to viewDidAppear as per comments

Display a Splash Screen Before Loading the Root Navigation Controller - iPhone

I am new bee in iPhone. I have implmented using some tutorials, a Splash Screen before loading the UIViewController. Now i want to implement a NavigationController in my application and want to display a Splash Screen before it. Since I am new in Iphone so i did not get any tutotrials or guides to make a Splash Screen before loading a Root Navigation Controller.
I have seen many methods in which they over write the Default.png file and so on. I dont want to implement that one. I want a sperate UIView to have my custom Images and text in it and display that UI View as a Splash Screen
can anybody Guide me please.
Thanks in advance
Here you go buddy. Have fun and happy coding....
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//Add a splash screen
UIImageView *imgv = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"splash.png"]];
imgv.userInteractionEnabled = YES;
[navigationController.view addSubview:imgv];
[imgv release];
[self performSelector:#selector(removeSplash:) withObject:imgv afterDelay:3.0];
[window addSubview:navigationController.view];
return YES;
}
- (void)removeSplash:(UIImageView *)imageView {
[imageView removeFromSuperview];
}
Use "self.window" to display the splash image first. If u simply write "window", the image will not be displayed and animated in the first view, since the image can't be directly linked to the window in that case. Write the following code in appdelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
MasterViewController *masterViewController = [[MasterViewController alloc] initWithNibName:#"MasterViewController" bundle:nil];
self.navigationController = [[UINavigationController alloc] initWithRootViewController:masterViewController];
self.window.rootViewController = self.navigationController;
[self.window makeKeyAndVisible];
UIImageView *imgv = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"themes.png"]];
imgv.userInteractionEnabled = YES;
[self.navigationController.view addSubview:imgv];
//[imgv release]; If you don't use ARC, uncomment this.
[self performSelector:#selector(removeSplash:) withObject:imgv afterDelay:3.0];
[self.window addSubview:self.navigationController.view];
return YES;
}
- (void)removeSplash:(UIImageView *)imageView
{
[imageView removeFromSuperview];
}

How to switch from one UIViewController to another?

I have a problem with switching from one viewcontroller to another.
Here is what I have :
In my "AppDelegate_iPad.m" :
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
CGRect frame = [UIScreen mainScreen].bounds;
window = [[UIWindow alloc] initWithFrame:frame];
window.backgroundColor = [UIColor blackColor];
LoginViewController_iPad *loginView = [[LoginViewController_iPad alloc] initWithNibName:#"LoginViewController_iPad" bundle:nil];
[window addSubview:loginView.view];
[self.window makeKeyAndVisible];
return YES;
}
That works. Now I have a login button on that screen and when pressed it calles this :
- (IBAction)doPressLoginButton
{
DebugLog(#"doPressLoginButton");
MenuViewController_iPad *menuView = [[MenuViewController_iPad alloc] initWithNibName:#"MenuViewController_iPad" bundle:nil];
[self.navigationController pushViewController:menuView animated:YES];
[menuView release];
}
The problem is that nothing happends. I assume I am missing the actual navigationconroller ?!
Hope that anyone can help.
Thank you
You should create a UINavigationController instance variable in your app delegate. Then, make sure you synthesize it and release it in your dealloc method.
Your implementation of application:didFinishLaunchingWithOptions: could look like this:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// window is usually an IBOutlet
CGRect frame = [UIScreen mainScreen].bounds;
window = [[UIWindow alloc] initWithFrame:frame];
window.backgroundColor = [UIColor blackColor];
LoginViewController_iPad *loginView = [[LoginViewController_iPad alloc] initWithNibName:#"LoginViewController_iPad" bundle:nil];
navigationController = [[UINavigationController alloc] initWithRootViewController:loginView];
[loginView release];
[self.window addSubview:navigationController.view];
[self.window makeKeyAndVisible];
return YES;
}
Now you'll be able to use self.navigationController.
I assume I am missing the actual navigationconroller ?!
you are right. :D
self.navigationController returns nil if you don't set up an NavigationController.
And any messages send to nil object will be ignored.
If you only need switch from one to another.
using
[self presentModalViewController:modalViewController animated:YES];
instead.