New UIViewController frame is 320x480 instead of larger screen size - ios

I updated Xcode today.
AppDelegate.m:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.vc = [[ViewController alloc] init];
UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:self.vc];
self.window.rootViewController = nc;
[self.window makeKeyAndVisible];
return YES;
}
ViewController.m:
- (void)viewDidLoad {
[super viewDidLoad];
CGRect f = self.view.frame;
f =[[UIScreen mainScreen] bounds];
self.view.frame = f;
self.view.backgroundColor = [UIColor redColor];
}
The frame is showing as 320x480. I can clearly see more space around the view, but it's black. Why is the view not filling out? There is no XIB.

Related

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

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:

self.window setRootViewController not working on xcode 6.1.1

I am facing some issues w.r.t XCode 6.1.1 , here is my code
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[[DBManager getSharedInstance] initWithdbFile];
[[DBManager getSharedInstance] createtableScripts];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
containerViewController =[[CprViewController alloc] init];
[containerViewController Setup];
[self.window setRootViewController:containerViewController.containerViewController];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
[self pickModel];
return YES;
}
Now the app works fine in older XCode i.e 5.1.1 but while simulating on XCode 6.1.1 the app launches the Launch image and there is no response i.e the execution ends at [self.window setRootViewController:containerViewController.containerViewController]; and it doesnt proceed to execute further lines of code. It shows no error in console. (The app however runs on device having latest OS (i.e iOS 8). Please help.
*Edited Post -- begins here
Thanks for helping out. Here’s the problem that i am facing,
This is in my AppDelegate file
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[[DBManager getSharedInstance] initWithdbFile];
[[DBManager getSharedInstance] createtableScripts];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; //Now Removed!!!
containerViewController =[[CprViewController alloc] init];
[containerViewController Setup];
self.window.rootViewController = containerViewController.containerViewController;
self.window.backgroundColor = [UIColor whiteColor];//Now Removed!!!
[self.window makeKeyAndVisible];//Now Removed!!!
[self pickModel];
return YES;
}
Now i have removed the three lines just as you suggested and now the execution continues
Now this is my code for [containerViewController Setup];
-(void)Setup
{
SideMenuViewController *leftMenuViewController = [SideMenuViewController homeBookViewController];
CprStartPageViewController *accounts=[[CprStartPageViewController alloc] initWithNibName:#"CprStartPageViewController" bundle:nil];
self.navController= [[CprNavigationViewController alloc]
initWithRootViewController:accounts];
self.containerViewController = [MFSideMenuContainerViewController containerWithCenterViewController:self.navController leftMenuViewController:leftMenuViewController
rightMenuViewController:nil];
[leftMenuViewController.passBookTable selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:YES scrollPosition:0];
}
The above code attaches a side menu (here i am attaching a left side menu) - No error in this code i.e the execution continues.
Now CprLoginViewController is my parentViewController
i.e the parentViewController for the 4 screen that i had mentioned and this is my initWithNibName for the 4 screens
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil parentViewController:(CprLoginViewController*) inVC
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil parentViewController:inVC];
if (self) {
// Custom initialization
}
return self;
}
and the error now occurs in CprLoginViewController where i am using a procedure called showNextPage to navigate between these 4 pages
-(void) showNextPage
{
if (currentPage > numberPages)
return;
CprLoginSubViewController *controller;
switch(currentPage)
{
case 0:
controller = [[Corppersondtl alloc] initWithNibName:#"Corppersondtl" bundle:nil parentViewController:self];
break;
case 1:
controller = [[CprLoginSub1ViewController alloc] initWithNibName:#"CprLoginSub1ViewController" bundle:nil parentViewController:self];
break;
case 2:
controller = [[CprLoginSub2ViewController alloc] initWithNibName:#"CprLoginSub2ViewController" bundle:nil parentViewController:self];
break;
case 3:
controller = [[CprLoginSub3ViewController alloc] initWithNibName:#"CprLoginSub3ViewController" bundle:nil parentViewController:self];
break;
case 4:
controller = [[CprLoginSub4ViewController alloc] initWithNibName:#"CprLoginSub4ViewController" bundle:nil parentViewController:self];
default:
break;
}
CGRect rBounds;
// add the required controller's view to scroll view
if (controller.view.superview == nil)
{
CGRect frame = self.scrollView.frame;
frame.origin.x = CGRectGetWidth(frame) * (currentPage);
frame.origin.y = 0;
controller.view.frame = frame;
[self addChildViewController:controller];
[self.scrollView addSubview:controller.view];
[controller didMoveToParentViewController:self];
// just scroll to appropriate page
rBounds = self.scrollView.bounds;
rBounds.origin.x = CGRectGetWidth(rBounds) * currentPage;
[self.scrollView scrollRectToVisible:rBounds animated:YES];
}
NSLog(#"scrollView is %lf", self.scrollView.frame.origin.y);
viewOriginalbounds = rBounds; //self.scrollView.frame;
currentPage++;
}
In the above code i am attaching a scrollview to scroll the screen upwards when keyboard appears for text inputs.
Now the line if (controller.view.superview == nil) is throwing exc_bad_access error.
Just delete that lines of code
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];

Overriding loadView and setting rootViewController programmatically, no Storyboard

I'm trying to create and set a View as the root View programmatically. I'm not using a Storyboard or ARC.
I'm trying to set a UIWebView as my ViewController's root View, which I'm unable to do. When I run the following code, I see a NavigationBar with a blank white screen. I tried setting a regular UIView as the root View and set its background color to blue, which loaded, so the problem seems to be specific to a UIWebView.
I'd really appreciate it if someone could point out what I'm doing wrong. Thanks!
Here is my loadView code:
// in GmailOAuthViewController.m
- (void)loadView {
CGRect frame = [[UIScreen mainScreen] bounds];
UIWebView *webView = [[[UIWebView alloc] initWithFrame:frame] autorelease];
self.view = webView;
}
And here is my application:didFinishLaunchingWithOptions: code:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
CGRect frame = [[UIScreen mainScreen] bounds];
UIViewController *viewController = [[GmailOAuthViewController alloc] init];
self.navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
self.window = [[UIWindow alloc] initWithFrame:frame];
self.window.backgroundColor = [UIColor whiteColor];
self.window.rootViewController = self.navigationController;
[self.window makeKeyAndVisible];
return YES;
}
you are assigning self.view...
if you assign webView to view everything else is gone
try
[self.view addSubview: webView]
this is code i just tried and it works
CGRect frame = [[UIScreen mainScreen] bounds];
UIViewController *viewController = [[ViewController alloc] init];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
self.window = [[UIWindow alloc] initWithFrame:frame];
self.window.backgroundColor = [UIColor whiteColor];
self.window.rootViewController = navigationController;
[self.window makeKeyAndVisible];
in ViewController.m
-(void)viewDidAppear:(BOOL)animated{
CGRect frame = self.view.bounds;
UIWebView *webView = [[UIWebView alloc] initWithFrame:frame] ;
webView.delegate = self;
self.view = webView;
NSString *thePath = [[NSBundle mainBundle] pathForResource:#"ViewControllerCatalog" ofType:#"pdf"];
if (thePath) {
NSData *pdfData = [NSData dataWithContentsOfFile:thePath];
[(UIWebView *)self.view loadData:pdfData MIMEType:#"application/pdf"
textEncodingName:#"utf-8" baseURL:nil];
}
}
I apologize I told you wrong earlier you do in fact assign to view

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.

Resources