How to programmatically create UIView so that it does not overlap the status bar - ios

I am using the following code in my app delegate to programmatically create the application's window and root controller:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.rootViewController = [[UIViewController alloc] init];
self.window.rootViewController.view = [[UIView alloc] init];
self.window.rootViewController.view.backgroundColor = [UIColor redColor];
return TRUE;
}
The code works fine, however the view associated to the root view controller overlaps the status bar, which I would like to avoid (see picture below).
How should I proceed? In particular I want to have a lightgray background for the status bar (similar to the apps created with a storyboard) and also handle orientation change correctly.
Thanks

Well, you can do something as following:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.rootViewController = [[UIViewController alloc] init];
self.window.rootViewController.view.backgroundColor = [UIColor whiteColor];
UIView *redView = [[UIView alloc] initWithFrame:CGRectMake(0,
[UIApplication sharedApplication].statusBarFrame.size.height,
[UIScreen mainScreen].bounds.size.width,
[UIScreen mainScreen].bounds.size.height)];
redView.backgroundColor = [UIColor redColor];
[self.window.rootViewController.view addSubview:redView];
[self.window makeKeyAndVisible];
return TRUE;
}
should look like this:

Related

How to disable swipe gesture of UINavigation Controller

In App Delegate:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
WalkThrough *viewControllers=[[WalkThrough alloc]init];
self.navigationController = [[UINavigationController alloc] initWithRootViewController:viewControllers];
[self.window setRootViewController:self.navigationController];
self.navigationController.interactivePopGestureRecognizer.enabled = NO;
navigationController.navigationBar.hidden = YES;
self.window.backgroundColor = [UIColor clearColor];
[self.window makeKeyAndVisible];
return YES;
}
Try to disable interactivePopGestureRecognizer after [window makeKeyAndVisible].
The key to the problem is that interactivePopGestureRecognizer property is nil until both two conditions are met:
the navigationController is associated to the window
the window become key and visible

(Objective C) When I make a new instance of a class inheriting from UIView using initWithFrame, why is the position not showing correctly?

In my appdelegate.m:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
self.window = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen] bounds]];
CGRect newFrame = CGRectMake(100, 200, 50, 150);
OtherView *newView = [[OtherView alloc] initWithFrame:newFrame];
//UIView *newView = [[UIView alloc] initWithFrame:newFrame];
newView.backgroundColor = [UIColor redColor];
[self.window addSubview:newView];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
OtherView is simply a blank class that inherits from UIView.
When I use the commented out code instead of Otherview, my CGRect is in the correct position, but when I use the code above as is, my CGRect is off-screen on the top left.
Try clean project CMD+SHIFT+K, you have not import OtherView.h or wrong inherits UIView

How can i connect UITableView to my Appdelegate.m?

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
MainViewController *rootViewController = [[MainViewController alloc] init];
_naviControl = [[UINavigationController alloc]initWithRootViewController:rootViewController];
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
[self.window addSubview:_naviControl.view];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
In my AppDelegate ..!
Connect to MainViewController.m
The AppDelegate is not a UIViewController so connecting a UITableView to it conflicts with the MVC principles as described here: https://developer.apple.com/library/ios/documentation/general/conceptual/devpedia-cocoacore/MVC.html
what you want to do is make your MainViewController's view have a tableView as a subview
something like this:
(in MainViewController.m)
- (void)viewDidLoad {
[super viewDidLoad];
self.feedTableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
self.feedTableView.dataSource = self;
self.feedTableView.delegate = self;
[self.view addSubview:self.feedTableView];
}

Add Original Custom View to the Window

I added my custom view to the window but it doesn't work. I tried to figure it out but it doesn't work well. (I created this project with the empty project template without using storyboards). This screen is supposed to show the red rectangle towards the bottom right corner of the screen.
I wanted to add my custom view showing red screen to the window, but it just shows the white screen.
AppDelegate.m:
//
// HypnosisterAppDelegate.m
// Hypnosister
//
// Created by TSH on 12/1/13.
// Copyright (c) 2013 TSH. All rights reserved.
//
#import "HypnosisterAppDelegate.h"
#import "HypnosisterViewController.h"
#import "HypnosisView.h"
#implementation HypnosisterAppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
HypnosisterViewController *test = [[HypnosisterViewController alloc] initWithNibName:#"HypnosisterViewController" bundle:nil];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:test];
self.window.rootViewController = nav;
CGRect viewFrame = CGRectMake(16, 24, 10, 15);
HypnosisView *view = [[HypnosisView alloc] initWithFrame:viewFrame];
[view setBackgroundColor:[UIColor redColor]];
[[self window] addSubview:view];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
#end
[edit]
I just messed up the ordering.
- (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];
HypnosisterViewController *test = [[HypnosisterViewController alloc] initWithNibName:#"HypnosisterViewController" bundle:nil];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:test];
self.window.rootViewController = nav;
CGRect viewFrame = CGRectMake(160, 240, 100, 150);
HypnosisView *view = [[HypnosisView alloc] initWithFrame:viewFrame];
[view setBackgroundColor:[UIColor redColor]];
[[self window] addSubview:view];
return YES;
}
As you set navigationcontroller as rootviewcontroller so HypnosisterViewController will appear every time no doubts. If you want to add a custom view to have to add as a subview on the HypnosisterViewController.
HypnosisView *view = [[HypnosisView alloc] initWithFrame:viewFrame];
[view setBackgroundColor:[UIColor redColor]];
[test.view addSubview:view];
Hopefully it will work
#toshi You code is correct just you missed the ordering .Use this code it will work.Let me know if it doesn't work.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
HypnosisterViewController *test = [[HypnosisterViewController alloc] initWithNibName:#"HypnosisterViewController" bundle:nil];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:test];
self.window.rootViewController = nav;
[self.window makeKeyAndVisible];
CGRect viewFrame = CGRectMake(16, self.window.frame.size.height - 24, 10, 15);
HypnosisView *view = [[HypnosisView alloc] initWithFrame:viewFrame];
[view setBackgroundColor:[UIColor redColor]];
[[self window] addSubview:view];
self.window.backgroundColor = [UIColor whiteColor];
return YES;
}
There is two points
1- The coordinate of the view in iOS begin from top-left so this frame (16, 24, 10, 15) will in the top-left of the view as frame calculated as (x,y,width,height)
2- Window does not responds to device Orientation. It will be always Portrait. If you rotate your device you will need to re-calculate the view Coordinate.

Cant Add UIImageView to self.window in didFinishLaunchingWithOptions

I am attempting to add an UIImageView as the first view that is shown after my Default.png disappears. This should be super easy but for some reason I am getting a black screen.
// AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
UIImageView *newimageview = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
[newimageview setImage:[UIImage imageNamed:#"myimage.png"]];
[_window addSubview:newimageview];
[_window bringSubviewToFront:newimageview];
}
I also tried setting the background color of newimageview to see if I just wasn't loading my image file properly, but the screen still appeared black.
Any help would be appreciated, I feel pretty dumb at the moment.
I just created a new project and modified only the didFinishLaunchingWithOptions method. Here is exactly the code I am using. I changed nothing else in the project:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
UIImageView *newimageview = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"bg-basketball"]];
[self.window addSubview:newimageview];
[self.window bringSubviewToFront:newimageview];
// Override point for customization after application launch.
//self.window.backgroundColor = [UIColor whiteColor];
//[self.window makeKeyAndVisible];
return YES;
}
And i am still getting the same black screen result.
I have added bg-basketball.png and bg-basketball#2x.png to my project and can see both images in XCode.
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.viewController = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];//Add this line
This is works for me.

Resources