initWithFrame is given empty frame - ios

I'm attempting to use a UINavigationController that utilizes a custom subclass of UINavigationBar. However, I'm running into a problem with initWithFrame. When initWithFrame is called, I print out the dimensions and it shows a 0.0 width and 0.0 height. But, when I print out the same frame in -(void) layoutSubviews inside of the UINavigationBar subclass, the frame is correctly set. Why is initWithFrame receiving a zero-sized frame, and what can I do to fix this? It doesn't sound correct to be manually calling initWithFrame or anything like that, since UINavigationController should take care of that. Here is the relevant code:
AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
UINavigationController *navController = [[UINavigationController alloc] initWithNavigationBarClass:[CustomNavigationBar class] toolbarClass:nil];
CoolViewController *vc = [[CoolViewController alloc] init];
[navController pushViewController:vc animated:YES];
self.window.rootViewController = navController;
[self.window makeKeyAndVisible];
return YES;
}
CustomNavigationBar.m
- (id)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame])
{
NSLog(#"initWithFrame with frame: %f, %f", frame.size.width, frame.size.height); // This prints "initWithFrame with frame: 0, 0"
}
return self;
}
Thank you!

I believe the navigation controller doesn't actually set the frame size to a specified value. Instead, it asks the navigation bar for its sizeThatFits: and uses that. By definition, that can't be done and passed to initWithFrame:. So, you want to use a combination of sizeThatFits:, setFrame: and layoutSubviews to add, size and reset your navigation bar content.

Related

Status bar issue using xib

I have created two ViewCrontroller using xib and set orientation of UIViews is LandscapeLeft.
and than add this code to
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
The following is my ideal result:
I'd like the window under the status bar in ios 7,
The first view, the result is what I want to get, however, when it jumps to the second view, there is a problem! The window move 20px to the left. how to fit it into the normal view?
3.Most impornatant condition is Autolayout is "TRUE".
my testing code:
https://www.dropbox.com/s/bj9wg4hwar1t8y0/statusbartesting.zip
Can anyone help to solve this problem?
You don't need to change window frame in AppDelegate.
I'll tell you code, please follow it
Appdelegate.m
- (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];
self.mxfirstciewcontroller = [[firstViewController alloc] initWithNibName:#"firstViewController" bundle:nil];
self.window.rootViewController = [[UINavigationController alloc] initWithRootViewController:self.mxfirstciewcontroller];
[(UINavigationController *)self.window.rootViewController setNavigationBarHidden:YES];
return YES;
}
In firstViewController.m
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[self.view setFrame:CGRectMake(0, 20, self.view.frame.size.width, self.view.frame.size.height)];
}
In secondViewController.m
- (void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
[self.view setFrame:CGRectMake(20, 0, self.view.frame.size.width, self.view.frame.size.height)];
}

CGRectMake label doesn't appear

I've just started to make a new app with a navigation bar. I made the first view with the navigation in the AppDelegate.m.
AppDelegate.m :
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
sleep(2);
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
UINavigationController* nav = [[UINavigationController alloc] initWithRootViewController:[[HomeViewController alloc] initWithNibName:#"HomeViewController" bundle:nil]];
self.window.rootViewController = nav;
[self.window makeKeyAndVisible];
return YES;
}
Then, I making the view of the HomeViewController inside loadView method.
LoadView of HomeViewController :
- (void)loadView
{
self.view = [[UIView alloc] init];
self.view.backgroundColor = [UIColor whiteColor];
self.searchPoiInstructionsLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 50, 20)];
self.searchPoiInstructionsLabel.layer.borderColor = [UIColor blackColor].CGColor;
self.searchPoiInstructionsLabel.layer.borderWidth = 2.0f;
self.searchPoiInstructionsLabel.text = #"HELLO WORLD";
[self.view addSubview:self.searchPoiInstructionsLabel];
}
My label Hello World doesn't appear...
For the frame, if I set CGRectMake(0, 65, 50, 20) , 65 for y, my "Hello World" appears... (I know, I have to increase the width :) )
I just don't understand the origins of my view... If someone can explain me please.
Thanks
This is iOS7 baggage. If you run your app under iOS6, you will see the label as you expect/intend.
Under iOS7, your view originates under the navigation bar which is why you don't see the label when you set the frame to originate at 0,0. If you look really closely, you can actually see the blurred label behind the Carrier indicator in the status bar.
There are multiple ways to work with this:
If the UINavigationBar is not translucent, then the 0,0 origin will work.
You can update the labels frame using topLayoutGuide
You could use a UIScrollView instead of UIView
Origin in iOS 7 is kind of different. In a Navigation Controller, things below Navigation bar can be showed a little with blur effect. So the label's origin in the Navigation Controller is up-left (0, 0) point below the Navigation bar instead of the leftest point under the Navigation bar. Here a demo for you.
- (void)viewDidLoad
{
[super viewDidLoad];
if ([[[[UIDevice currentDevice]systemVersion] componentsSeparatedByString:#"."][0] intValue] >=7) {
if ([self respondsToSelector:#selector(edgesForExtendedLayout)]) {
self.edgesForExtendedLayout = UIRectEdgeNone;
}
}
}

Black empty area below table in iOS7 with nested UIViewControllers

I get some black empty area below a UITableViewController when nested in a certain way (iOS7). Would anybody know why that happens?
(obviously the code is a 100% stripped down version of the actual app's code)
- (BOOL) application:(UIApplication *) application didFinishLaunchingWithOptions:(NSDictionary *) launchOptions {
UITabBarController *tabBarController = [UITabBarController new];
UITableViewController *demoViewController = [UITableViewController new];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:demoViewController];
tabBarController.tabBar.translucent = NO;
navigationController.navigationBar.translucent = NO;
// THESE LINES INTRODUCE A BLACK AREA BELOW THE TABLE
InBetweenViewController *inBetweenViewController = [InBetweenViewController new];
[inBetweenViewController addChildViewController:navigationController];
[inBetweenViewController.view addSubview:navigationController.view];
tabBarController.viewControllers = #[ inBetweenViewController ];
// INSTEAD, THIS LINE WORKS CORRECTLY
/* tabBarController.viewControllers = #[ navigationController ]; */
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.rootViewController = tabBarController;
[self.window makeKeyAndVisible];
return YES;
}
#implementation InBetweenViewController
- (void) viewWillLayoutSubviews {
[super viewWillLayoutSubviews];
for (UIView *subview in self.view.subviews) {
subview.frame = self.view.bounds;
}
}
#end
It's because your inBetweenViewController doesn't know how to render its childViewController. You just add the the view of that controller without any further instructions. It should be possible to solve this by using a simple autoresizingMask. You then also need to make sure that the subview's size is the same as the superview's size when you add it. If you need more details on how to do that, let me know.

Adding UIImageView to rootViewController

All I am simply trying to do is display an image on the screen as I am just starting out iOS
development. I figured since UIImageView is a subclass of UIView, I would add it in a similar way but I am not having any luck. I understand this is an easy question but any help would be appreciated.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: (NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.rootViewController = [UIViewController new];
UIImage* stallion = [UIImage imageNamed:#"stallion1.png"];
UIImageView* iv = [UIImageView alloc];
iv.image = stallion;
[self.window.rootViewController.view addSubview:iv];
[self.window makeKeyAndVisible];
return YES;
}
Try something like this:
App delegate:
#import "RootViewController.h"
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
//allocate and initialize the root view controller
RootViewController *rootViewController = [[RootViewController alloc] init];
//set the root view controller
self.window.rootViewController = rootViewController;
[self.window makeKeyAndVisible];
return YES;
}
Create a subclass of UIViewController called RootViewController
.h
#import <UIKit/UIKit.h>
//set the class RootViewController as a subcalss of UIViewController
#interface RootViewController : UIViewController
#end
.m
//this is one of the life cycle methods of a UIViewController and should already be in the code when the class is created
- (void)viewDidLoad
{
//execute the viewDidLoad method of the superclass (UIViewController)
[super viewDidLoad];
//allocate and initialize the image view
//assign the image view a frame
//x offset from the left = 0.0
//y offset from the top = 0.0
//width = the view controller's view's width (should be the whole screen)
//height = the view controller's view's height(should be the whole screen)
UIImageView *iv = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, self.view.frame.size.width, self.view.frame.size.height)];
[iv setImage:[UIImage imageNamed:#"myImageName.png"]];
//the background will be red if everything is setup correctly, but the image isn't found
[iv setBackgroundColor:[UIColor redColor]];
//add the image view to the view controller's view
[self.view addSubview:iv];
}
//EDIT
It would be very helpful for you if u will read book in which there are information about View Controllers. This is very essential information. I recommend you to read "The Core IOS 6 Cookbook" by Erica Sadum. It really helped me.
You can also just look at the apple documentation http://developer.apple.com/library/ios/#documentation/WindowsViews/Conceptual/ViewControllerCatalog/
Write something like this in AppDelegate.m:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
MyViewController *viewController = [[MyViewController alloc] init];
self.window.rootViewController = viewController;
[self.window makeKeyAndVisible];
return YES;
}
Next you must add the UIImageView but only when the view of the created view controller is loaded. The best method for this is viewDidLoad. You can also use viewDidAppear: or viewWillAppear:.
You must override this methods in MyViewController class (which should inherit from the UIViewController class).
- (void)viewDidLoad
{
[super viewDidLoad];
UIImageView *imageView = [UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, self.frame.size.width, self.frame.size.heigth);
imageView.image = [UIImage imageNamed#"stallion1.png"];
[self.view addSubview:imageView];
}

Creating a view controller without a nib

In AppDelegate, I want to create a UIViewController subclass and add it's view. The viw itself will be specified in code - there is no nib.
Based on the apple docs, I should use
initWithNibName:nil bundle:nil];
and then in loadView of the controller, I add my subviews etc.
However, the follwing test code below does not work for me. I modelled the AppDelegate code on Apple's PageControl demo, simply because my app will implement a similar structure (specifically a base controller to manage a paged scroll view, and an array of other controller's to build the pages).
But I suspect my AppDelegate code is the problem, since logging proves that initWithNibName:: and loadView both fire. The app as below runs, but the screen is blank. I am expecting a green view with a label.
AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
ScrollerController *controller = [[ScrollerController alloc] initWithNibName:nil bundle:nil];
[self.window addSubview:controller.view];
[self.window makeKeyAndVisible];
return YES;
}
ScrollerController (the UIViewController subclass)
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)loadView{
CGRect applicationFrame = [[UIScreen mainScreen] applicationFrame];
UIView *contentView = [[UIView alloc] initWithFrame:applicationFrame];
contentView.backgroundColor = [UIColor greenColor];
self.view = contentView;
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(40, 40, 100, 40)];
[label setText:#"Label created in ScrollerController.loadView"];
[self.view addSubview:label];
}
Try to use:
self.window.rootViewController = controller;
instead of
[self.window addSubview:controller.view];
Note, that you should also #synthesize window; and create it
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
Instead of initWithNibNamed:, just use alloc and init or any of the other designated initalizers for the view controller. Here is an example from a project
hoverViewController=[[BDHoverViewController alloc] initWithHoverStatusStyle:BDHoverViewStatusActivityProgressStyle];
self.window.rootViewController=hoverViewController;
[self.window makeKeyAndVisible];
also, the correct form( for now anyways) for adding the root view controller to the window in the app delegate is like this:
self.window.rootViewcontroller=controller;
[self.window makeKeyAndVisible];
You don't need to add the view to the window. The above code does it automatically.
Good luck,
T

Resources