NOTE: Jump to end for answer.
I am new to IOS Programming. How can I add a visible UIView to the following code. So far it just has the nav bar appear, and black space below, no imageView or the square UIView in the code below.
Below is code in my HomeScreenViewController
- (void)viewDidLoad
{
[super viewDidLoad];
UIImageView *friendsIcon = [[UIImageView alloc] initWithImage:([UIImage imageWithContentsOfFile:#"change_user-512"])];
[friendsIcon setFrame:CGRectMake(0, 0, 100, 100)];
[self.view addSubview:friendsIcon];
UIView *square = [[UIView alloc] initWithFrame:CGRectMake(200, 200, 100, 100)];
[square setTintColor:[UIColor orangeColor]];
[self.view addSubview:square];
// Do any additional setup after loading the view.
}
Below is code in AppDelegate.
#implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.myViewController = [[HomeScreenViewController alloc] init];
UINavigationController *navBar = [[UINavigationController alloc] initWithRootViewController:self.myViewController];
navBar.title = #"Navigation";
self.window.rootViewController = navBar;
[self.window makeKeyAndVisible];
return YES;
}
EDIT:
Here is also the interface for appDelegate. This is literally the only code in the proj other than the generated lines upon creating the project in xcode. I also deleted the storyboard file along with its info in the plist file (otherwise the app would crash).
#import <UIKit/UIKit.h>
#import "HomeScreenViewController.h"
#interface AppDelegate : UIResponder <UIApplicationDelegate>
#property (strong, nonatomic) UIWindow *window;
#property (nonatomic, strong) HomeScreenViewController *myViewController;
#end
EDITAgain:
No Need to Read Comments, just read this:
Yes, viewDidLoad does invoke.
The background changes to red if i set background color.
No I am not using storyboard.
Still unanswered.
http://postimg.org/image/h5ayao2q3/
FINAL EDIT: Finally! problem solved. It was as simple as setting the background colour for the UIView square. D'oh.
- (void)loadView
{
[super loadView];
self.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[self.view setBackgroundColor:[UIColor redColor]];
UIImageView *friendsIcon = [[UIImageView alloc] initWithImage:([UIImage imageWithContentsOfFile:#"change_user-512"])];
[friendsIcon setFrame:CGRectMake(0, 0, 100, 100)];
[self.view addSubview:friendsIcon];
UIView *square = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
[square setBackgroundColor:[UIColor orangeColor]];
[self.view addSubview:square];
// Do any additional setup after loading the view.
}
Thank you very much everyone for your help. I guess my UIImage is borked, but I can figure that out myself.
Try changing it:
self.myViewController = [[HomeScreenViewController alloc] init];
TO:
self.myViewController = [[HomeScreenViewController alloc] initWithNibName:#"HomeScreenViewController" bundle:nil];
And Make sure there are some label or some thing in HomeScreenViewController. Other part of your code looks fine. Hope this helps. :)
First put breakpoint and verify that ViewDidLoad method is getting called.
If not , since you mentioned that you are creating the views programmatically you need to override the - (void)loadView method and create the view for the view controller.
-(void)loadView {
self.view = [[UIView alloc] init];
UIImageView *friendsIcon = [[UIImageView alloc] initWithImage:([UIImage imageWithContentsOfFile:#"change_user-512"])];
[friendsIcon setFrame:CGRectMake(0, 0, 100, 100)];
[self.view addSubview:friendsIcon];
UIView *square = [[UIView alloc] initWithFrame:CGRectMake(200, 200, 100, 100)];
[square setBackgroundColor:[UIColor orangeColor]];
[self.view addSubview:square];
}
Changed the line [square setTintColor:[UIColor orangeColor]]; to [square setBackgroundColor:[UIColor orangeColor]];
right click your UIViewController and check view connection is established to the xib or storyboard view.
ViewDidload will not get called unless view loads from storyboard or xib.
loadView method gets called if you are creating UIView programatically
If you are not using story board or XIB then you need to add the following method
-(void)loadView{
//load the views
}
I have two options to solve this problem:
1) If you have XIB file corresponding to the controller:
HomeScreenViewController *homeScreenViewController = [[HomeScreenViewController alloc] initWithNibName:#"HomeScreenViewController"] bundle:nil];
1a. Verify the name of your XIB is correct.
1b. Goto XIB file of HomeScreenViewController -> File Owner -> Connection Inspector -> Check the view outlet is connected.
2) If you do it programmatically, add this to the end of your loadView function:
self.view = square;
so the final code should look like:
- (void)loadView
{
//[super loadView];
self.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; // <-- look at here, your view has changed its frame now
[self.view setBackgroundColor:[UIColor redColor]];
UIImageView *friendsIcon = [[UIImageView alloc] initWithImage:([UIImage imageWithContentsOfFile:#"change_user-512"])];
[friendsIcon setFrame:CGRectMake(0, 0, 100, 100)];
[self.view addSubview:friendsIcon];
UIView *square = [[UIView alloc] initWithFrame:CGRectMake(200, 200, 100, 100)];
[square setTintColor:[UIColor orangeColor]];
[self.view addSubview:square];
// Do any additional setup after loading the view.
}
Related
I have started to develop iOS without interface builder.
AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
ViewController *viewController = [[ViewController alloc] init];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.rootViewController = viewController;
[self.window makeKeyAndVisible];
return YES;
}
ViewController.m
- (void)loadView
{
CGRect applicationFrame = [[UIScreen mainScreen] applicationFrame];
UIView *contentView = [[UIView alloc] initWithFrame:applicationFrame];
contentView.backgroundColor = [UIColor whiteColor];
self.view = contentView;
}
- (void)viewDidLoad
{
[super viewDidLoad];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 100, 280, 20)];
[label setText:#"ViewController"];
[label setTextAlignment:NSTextAlignmentCenter];
[label setBackgroundColor:[UIColor redColor]];
[self.view addSubview:label];
}
I don't know why, but when I load application, there is space between top ant botton and view. Does anyone has an idea why?
You need to add the Default-568h#2x.png background image to the project.
If your Deployment Target is iOS 7.0 onwards just check this.
self.edgesForExtendedLayout = UIRectEdgeNone;
I hope it will work...
I get the NSException "unrecognized selector sent to instance" and then my app crashes whenever the btnReload UIBarButtonItem is pressed. I included the code for what the method does for the action, however the crash/exception occurs whether or not the testChangingViewController method has code.
All of the following code is in my UIViewController class.
- (void)loadView //use loadView because not a storyboard based GUI.
{
[super loadView];
UIBarButtonItem *btnReload =[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:#selector(testChangingViewController:)];
self.navigationController.topViewController.navigationItem.leftBarButtonItem = btnReload;
btnReload.enabled=TRUE;
//self.view = [[UIView alloc] initWithFrame:[[UIScr een mainScreen] bounds]];
[self.view setBackgroundColor:[UIColor whiteColor]];
[self setTitle:#"Navigation"]; //navigation bar title tied to the view controller it is managing.
UIImageView *friendsIcon = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"change_user-512"]];
[friendsIcon setFrame:CGRectMake(60, 130, 70, 70)];
[self.view addSubview:friendsIcon];
UIImageView *statusIcon = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"talk-512"]];
[statusIcon setFrame:CGRectMake(180,130, 70, 70)];
[self.view addSubview:statusIcon];
UIView *square = [[UIView alloc] initWithFrame:CGRectMake(250, 250, 50, 50)];
[square setBackgroundColor:[UIColor orangeColor]];
[self.view addSubview:square];
// Do any additional setup after loading the view.
}
-(void)testChangingViewController
{
UITableViewController *newViewController = [[UITableViewController alloc] initWithStyle:UITableViewStylePlain];
[self.navigationController pushViewController:newViewController animated:YES];
}
You assign the UIBarButtonItem the #selector(testChangingViewController:) (colon at end, one parameter) but you define the method testChangingViewController (no colon, no params). The selector it is looking for that takes a parameter is not present, hence your error. These target selectors always tend to have one parameter, the "sender", that is, the UIControl that generated the event.
I had created the the view programatically but it does not display on the screen
and gives a message in the console that
Failed to instantiate the default view controller for UIMainStoryboardFile 'Main' - perhaps
the designated entry point is not set?
Here is my code
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary
*)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] ;
exmTableViewController *vc =[[exmTableViewController alloc] init];
self.window.rootViewController=vc;
self.window.backgroundColor =[UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
*Code In Viewdidload *
- (void)viewDidLoad
{
[super viewDidLoad];
UIImageView *img =[[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 100, 20)];
self.imageView=img;
[self.view addSubview:self.imageView];
UILabel *lbl =[[UILabel alloc] initWithFrame:CGRectMake(10, 10, 100, 20)];
lbl.text =#"Hello";
self.view =lbl;
}
Instead this line self.view =lbl;, use [self.view addSubview:lbl]; to add subview to your view.
try this
- (void)viewDidLoad
{
[super viewDidLoad];
UIImageView *img =[[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 100, 20)];
self.imageView=img;
[self.view addSubview:self.imageView];
UILabel *lbl =[[UILabel alloc] initWithFrame:CGRectMake(10, 10, 100, 20)];
lbl.text =#"Hello";
[self.view addSubview:lbl];
}
-(void)viewDidLoad
{
[super viewDidLoad];
UIImageView *img =[[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 100, 20)]; [self.view addSubview:img];
UILabel *lbl =[[UILabel alloc] initWithFrame:CGRectMake(10, 200, 100, 20)];
lbl.text =#"Hello";
[self.view addSubview:lbl];
}
Try like this. self.view = lbl; in this line you assign self.view is as label you want to add subview [self.view addSubview:yourSubView]; do like this.
When you are creating View controller from storyboard, view controller is initiated by storyboard. You should not create windows root controller by yourself. You need to provide story board name in Plist.
After loading view viewDidLoad method will be called. Here you need to set up view(Adding and configuring subviews). But you are changing viewControllers view property, that is set by storyboard. If you want to change your view controllers view you can use,
- (void)loadView
// inside this method you can assign new view to view property.
If you are adding view as subview use view's method
- (void)addSubview:(UIView *)view
I try to set an uiview from the app delegate to an other viewcontroller, in viewcontroller I add an IBOutlet property and from app delegate I use this code:
self.secondViewController = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 500, 500)];
[view setBackgroundColor:[UIColor redColor]];
self.secondViewController.myView = view;
self.window.rootViewController = self.secondViewController;
[self.window makeKeyAndVisible];
but the view of my uiviewcontroller isn't red, but the default color that I give from IB.
where is the error?
I can't seem to print my second UILabel , however the first one gets printed. Any help is appreciated.
I am trying to print the yellow label on top of the blue uiview , like i have successfully done with the red label.
here is my code:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: (NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
CGRect frame = CGRectMake(10, 30, 100, 100);
UILabel *myLabel = [[UILabel alloc] initWithFrame:frame];
UILabel *myLabel2= [[UILabel alloc] initWithFrame:frame];
myLabel=[[UILabel alloc] initWithFrame:CGRectMake(50, 50, 100, 100)];
myLabel.backgroundColor = [UIColor redColor];
myLabel.text = #"Hello";
myLabel2=[[UILabel alloc] initWithFrame:CGRectMake(50,300,100,100)];
myLabel2.backgroundColor=[UIColor yellowColor];
myLabel2.text=#"World!";
UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(1, 1, 500, 500)];
UIView *myView1 = [[UIView alloc] initWithFrame:CGRectMake(65,50,200,200)];
UIView *myView2= [[UIView alloc] initWithFrame:CGRectMake(65,260,200,200)];
myView1.backgroundColor=[UIColor greenColor];
myView.backgroundColor=[UIColor cyanColor];
myView2.backgroundColor=[UIColor blueColor];
[myView addSubview:myView1];
[myView addSubview:myView2];
[myView2 addSubview:myLabel2];
[myView1 addSubview:myLabel];
[self.window addSubview:myView];
[self.window makeKeyAndVisible];
return YES;
}
thanks for any help.
"Actually your label is getting drawn outside the view area of myView2."
width and height of myView2 is 200,200.
myLabel2 to has y position of 300 which is larger that its superView(myView2)'s height.
:).
myLabel2 is drawn outside myView2's frame. Always y position of a view is taken relative to its superview.
Change this line
myLabel2=[[UILabel alloc] initWithFrame:CGRectMake(50,300,100,100)];
to
myLabel2=[[UILabel alloc] initWithFrame:CGRectMake(50,30,100,100)];
Reason is that your label2 is going outside your view
Hope this helps you..