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

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

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:

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

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.

adding UIViewcontroller's view to UIWindow does not show

I am trying to see a view with the following code where I am using storyboard however I did not make the uiviewcontroller's scene initial view controller.
This is the code I wrote in didFinishLaunchingWithOption of my AppDelegate.
UIWindow* window = [UIApplication sharedApplication].keyWindow;
abcViewController *controller = [[abcViewController alloc]init];
UIView *redView = [[UIView alloc] initWithFrame: CGRectMake ( [[UIScreen mainScreen] bounds].origin.x+30, [[UIScreen mainScreen] bounds].origin.y+30, 260, 400)];
[redView setBackgroundColor:[UIColor redColor]];
UIView *greenView = [[UIView alloc] initWithFrame: CGRectMake ( redView.frame.origin.x + 10.0f, redView.frame.origin.y + 10.0f, 180, 320)];
[greenView setBackgroundColor:[UIColor greenColor]];
[redView addSubview:greenView];
[controller.view addSubview:redView];
window.rootViewController = controller;
[window makeKeyAndVisible];
return YES;
First assign storyboard id to the abcViewController in storyboard eg."firstView".
Import the viewController in app delegate
In the storyboard, uncheck the "Is initial View Controller" attribute from the first view controller.
In the app's info.plist, remove the value of "Main storyboard file base name".
Instantiate the storyboard, create the window object and set initial view controller in the app delegate's application:didFinishLaunchingWithOptions: method
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds];
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
TestViewController *controller = [storyboard instantiateViewControllerWithIdentifier:#"firstView"];
UIView *redView = [[UIView alloc] initWithFrame: CGRectMake ( [[UIScreen mainScreen] bounds].origin.x+30, [[UIScreen mainScreen] bounds].origin.y+30, 260, 400)];
[redView setBackgroundColor:[UIColor redColor]];
UIView *greenView = [[UIView alloc] initWithFrame: CGRectMake ( redView.frame.origin.x + 10.0f, redView.frame.origin.y + 10.0f, 180, 320)];
[greenView setBackgroundColor:[UIColor greenColor]];
[redView addSubview:greenView];
[controller.view addSubview:redView];
self.window.rootViewController = controller;
[self.window makeKeyAndVisible];
return YES;
}

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

Resources