0

I am trying to add label (headerLabel) to another subview (introPadTutorial) here and it is not showing up on the screen. Can some please help me where am I making mistake?

//Header file

@property (nonatomic, retain) UIView *introPadTutorial;

//Implementation file

@synthesize introPadTutorial;

UIView *v = [_appDelegate getCurrentView];
CGRect tutorialFrame = [self getTableViewFrame];

introPadTutorial = [[UIView alloc] initWithFrame:CGRectMake(v.frame.size.width, 80, -tutorialFrame.size.width, v.frame.size.height)];
[introPadTutorial setBackgroundColor:[UIColor uberLightGray]];
[introPadTutorial setUserInteractionEnabled:YES];

UILabel *headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 180, 180)];
[headerLabel setText:@"test test test"];
[headerLabel setBackgroundColor:[UIColor greenColor]];
[headerLabel setFont:[UIFont boldSystemFontOfSize:16.0f]];
[headerLabel setTextColor:[UIColor redColor]];
[headerLabel setShadowColor:[UIColor whiteColor]];
[headerLabel setShadowOffset:CGSizeMake(0, 1)];

[self.introPadTutorial addSubview:headerLabel];
[headerLabel release];

[v addSubview:introPadTutorial];
[introPadTutorial release];
Lorenzo B
  • 33,216
  • 24
  • 116
  • 190
Kishore
  • 3
  • 1

3 Answers3

0

Is your introPadTutorial view showing up? You have a negative width in it's initWithFrame call.

Lorenzo B
  • 33,216
  • 24
  • 116
  • 190
Mike M
  • 4,358
  • 1
  • 28
  • 48
  • Yes my "introPadTutorial" view is showing up. But, If i add any subview to "introPadTutorial" is not showing up. – Kishore Aug 29 '12 at 18:29
  • Yep. the purpose of the negative width is to start drawing the view from top right corner of the super view. – Kishore Aug 29 '12 at 18:32
  • @Kishore maybe you need to do "self.introPadTutorial = [[UIView". might be running into a scoping issue. – Mike M Aug 29 '12 at 18:33
  • I have already tried with "self.introPadTutorial". it was not helping. – Kishore Aug 29 '12 at 18:37
0

May be your...

introPadTutorial = [[UIView alloc] initWithFrame:CGRectMake(v.frame.size.width, 80, -tutorialFrame.size.width, v.frame.size.height)];

should actually be...

introPadTutorial = [[UIView alloc] initWithFrame:CGRectMake(0, 80, tutorialFrame.size.width, v.frame.size.height)];
abix
  • 287
  • 3
  • 14
  • Thanks for your quick response. your suggestion is adjusting the "introPadTutorial" view. But still I am not able to see he "HeaderLable" – Kishore Aug 29 '12 at 18:30
  • Can you check if your headerLabel's frame is within the bound of introPadTutorial? Try this... if (CGRectContainsRect(introPadTutorial.bounds, headerLabel.frame)) { NSLog("Inside"); } else { NSLog("Outside"); } – abix Aug 29 '12 at 18:37
  • It says outside. I think that is why I am not able to see it. How do i bring it to inside? – Kishore Aug 29 '12 at 18:40
  • I am not sure about usage of the negative width, you may rather try positive values. Also try printing out the introPadTutorial.bounds's width, height etc to figure out what is the size of the view. – abix Aug 29 '12 at 18:44
  • Yes. You are correct. Lesson learned - I will not use negative width anymore (LOL). Thanks a lot.... – Kishore Aug 29 '12 at 20:04
0

Don't use a negative width to change the POSITION, use the origin to change the position.

Use this:

introPadTutorial = [[UIView alloc] initWithFrame:CGRectMake(v.frame.size.width - tutorialFrame.size.width, 80, tutorialFrame.size.width, v.frame.size.height)];

I think that should help make the subview actually show up.

Justin Paulson
  • 4,388
  • 1
  • 23
  • 28
  • Yes. You are correct. This code fixed my problem. Lesson learned - I will not use negative width anymore (LOL). Thanks a lot.... – Kishore Aug 29 '12 at 20:04
  • If you have a negative CGRect, use CGRectStandardize to fix it easily. – WolfLink Aug 30 '12 at 06:17