CGRectMake label doesn't appear - ios

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;
}
}
}

Related

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.

What is the best way to display a UIView at the top of the screen in either iOS 6.1 or 7?

Here is a simple program to display a blue colored view at the top of the screen.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];
UIViewController* vc = [[UIViewController alloc] init];
CGFloat y = 20;
if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_6_1) {
y = 0;
}
UIView* sample = [[UIView alloc] initWithFrame:CGRectMake(0, y, 768, 100)];
sample.backgroundColor = [UIColor blueColor];
[vc.view addSubview:sample];
self.window.rootViewController = vc;
[self.window makeKeyAndVisible];
return YES;
}
I have logic in there to set the y variable to either 0 or 20 if iOS version is 7 or 6 and under. Is there a better way to do this?
iOS 7 brings several changes to how you lay out and customize the appearance of your UI. The changes in view-controller layout, tint color, and font affect all the UIKit objects in your app. In addition, enhancements to gesture recognizer APIs give you finer grained control over gesture interactions.
Please refer, apple doc
In my case, I decided to do exactly as you've mentioned. It is not "as clean as", it takes more work, but it gives you a fine precision, in case you're not targeting too many devices.

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];
}

initWithFrame is given empty frame

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.

Use a UIImageView to draw a background image for UITableView results in artefacts

I'm very new to Xcode and not familiar with all functions.
I simply would like to have a background image underneath my UITabelView.
There are many answers to this problem, but all of them give me these white artefact borders.
So my question is, did anybody end up with the same problem and solved it?
To get an idea, imag is attached below.
Thx for any help!
Here just a quick view over my implementaion
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:
(NSDictionary *)launchOptions{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch
//initialise main views
UIImageView *backGround = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0,
self.window.bounds.size.width, self.window.bounds.size.height)];
[backGround setImage:[UIImage imageNamed:#"Default.png"]];
[self.window addSubview:backGround];
LoginViewController *loginViewController = [[LoginViewController alloc]
initWithNibName:#"LoginViewController" bundle:nil];
self.navigationController = [[UINavigationController alloc]
initWithRootViewController:loginViewController];
self.window.rootViewController = self.navigationController;
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
[Same Problem on antoher UI Item]
Now I have the same problem on my programmatically created UIButtons with rounded rect. But I have no idea how to get rid of them?
I face this problem before
i think u set your tableview separator property to Single Line Etched style, to solve this problem set your tableview separator in cellforrowatindexpath delegate :
tableview.separatorStyle = tableview.UITableViewCellSeparatorStyleSingleLine;
or
tableview.separatorStyle = tableview.UITableViewCellSeparatorStyleNone;
or u can set it in xib file under separator section.
may help you
Just a thought... But you take the size of [UIScreen mainScreen]. Since you have the ApplicationBar enabled your windows is 20px smaller in height...(?)

Resources