Label not getting printed to ios simulator objective c - ios

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..

Related

Two different approaches to subviews

Regarding subviews in ios, I have a view that covers the whole screen, then I create and import another view which is just a red square on top of it. I was wondering if there is any difference or advantadge between these two approaches:
Approach 1:
//set the view
UIView *myView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
myView.backgroundColor = [UIColor whiteColor];
[self.view addSubview:myView];
//setTheSquareView
CGRect firstFrame = CGRectMake(160, 240, 100, 150);
HypnosisView *firstView = [[HypnosisView alloc] initWithFrame:firstFrame];
firstView.backgroundColor = [UIColor redColor];
[self.view addSubview:firstView];
and approach 2:
//set the view
UIView *myView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
myView.backgroundColor = [UIColor whiteColor];
[self.view addSubview:myView];
//setTheSquareView
CGRect firstFrame = CGRectMake(160, 240, 100, 150);
HypnosisView *firstView = [[HypnosisView alloc] initWithFrame:firstFrame];
firstView.backgroundColor = [UIColor redColor];
[myView addSubview:firstView];
The only difference being that in the first case I add both views as subviews of the main property view whereas in the second case I add the second view as a subview of the first view. They look the same on screen.
Thanks
The difference in my opinion that on the first approach if you change any of (myView) constraints , firstFrame will not change but in the second approach it will.

No UIView appearing in the interface

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

how to show the views added in view controller

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

How can I add UIView object to self.view.window in ios7?

I'd like to add a dark view layer that covers the whole screen.
In order to do that, I added UIView object to window property of UIViewController as shown below.I remember I was able to add a view to window like this in ios6. But, it doesn't work in ios7.
How am I able to make it work?
Thanks in advance!!
- (void)viewDidLoad
{
[super viewDidLoad];
UIView *overlayView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
overlayView.backgroundColor = [UIColor blackColor];
overlayView.alpha = 0.8;
[self.view.window addSubview:overlayView];
}
Here's one way of doing what you want:
[[UIApplication sharedApplication].keyWindow addSubview:overlayView];
According to the documentation of UIView, the window property is nil if the view has not yet been added to a window which is the case when viewDidLoad is called.
You can do the same thing in viewDidAppear:(BOOL)animated;
- (void)viewDidAppear:(BOOL)animated; {
UIView *overlayView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
overlayView.backgroundColor = [UIColor blackColor];
overlayView.alpha = 0.8;
[self.view.window addSubview:overlayView];
}
Swift 3
UIApplication.shared.keyWindow?.addSubview(overlayView);
you can try like this
- (void)viewDidLoad
{
[super viewDidLoad];
UIView *overlayView = [[UIView alloc] initWithFrame:self.view.frame];
overlayView.backgroundColor = [UIColor blackColor];
overlayView.alpha = 0.8;
overlayView.center = self.view.center
[self.view addSubview:overlayView];
}
i hope will helpful for you...

UIScrollView and UINavigationController not working with iOS 4

i have a problem with UIScrollView and UINavigationController(s) and iOS 4. The following code works fine with iOS 5:
////////////////////////////////////////////////////////////////////////////////////
/* Scroll View */
////////////////////////////////////////////////////////////////////////////////////
mScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 88, 1024, 668)];
mScrollView.contentSize = CGSizeMake(1850, 668);
////////////////////////////////////////////////////////////////////////////////////
// First View
////////////////////////////////////////////////////////////////////////////////////
UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed: #"view_bg.png"]];
imgView.frame= CGRectMake(10, 50, 350, 510);
[mScrollView addSubview:imgView];
[imgView release];
mFirstView = [[[FirstView alloc] init] autorelease];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:mFirstView];
navigationController.navigationBar.tintColor = [UIColor colorWithRed:79.0/255 green:143.0/255 blue:0.0/255 alpha:1];
navigationController.view.frame = CGRectMake(25, 65, 320, 480);
[mScrollView addSubview:navigationController.view];
////////////////////////////////////////////////////////////////////////////////////
// Second View
////////////////////////////////////////////////////////////////////////////////////
UIImageView *imgView2 = [[UIImageView alloc] initWithImage:[UIImage imageNamed: #"view_bg.png"]];
imgView2.frame= CGRectMake(380, 50, 350, 510);
[mScrollView addSubview:imgView2];
[imgView2 release];
mSecondView = [[[SecondView alloc] init] autorelease];
UINavigationController *navigationController2 = [[UINavigationController alloc] initWithRootViewController:mSecondView];
navigationController2.navigationBar.tintColor = [UIColor colorWithRed:79.0/255 green:143.0/255 blue:0.0/255 alpha:1];
navigationController2.view.frame = CGRectMake(395, 65, 320, 480);
[mScrollView addSubview:navigationController2.view];
// finally add the scroll view to the parent view
[self.view addSubview:mScrollView];
All i do is, to add a ScrollView with 2 NavigationControllers (with a allocated root view controller) do the view. But using this code with iOS 4 does not show the right size of the ViewControllers added. The ViewControllers are the size of the whole iPad screen!!!!!
As i already said, it works on iOS 5! Even when i change the size of the views added manually it is not working, the added view always fills the whole iPad screen!! Any ideas to fix this?
Thank you in advance!
in your code you have the following
//first view you add mfirstview.view
[mScrollView addSubview:mFirstView.view];
//second view you add navigationController2.view
[mScrollView addSubview: navigationController2.view];
So i guess you have to change the first view code to do the following
[mScrollView addSubview: navigationController.view];

Resources