将子视图添加到另一个子视图不起作用

时间:2012-08-29 18:13:58

标签: iphone ios ipad view addsubview

我正在尝试将label(headerLabel)添加到另一个子视图(introPadTutorial),并且它没有显示在屏幕上。有些人可以帮助我,我在哪里弄错了?

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

3 个答案:

答案 0 :(得分:0)

您的introPadTutorial视图是否显示?您的initWithFrame电话的宽度为负。

答案 1 :(得分:0)

可能是你的......

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

实际应该是......

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

答案 2 :(得分:0)

不要使用负宽度来更改POSITION,使用原点来改变位置。

使用此:

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

我认为这应该有助于使子视图真正显示出来。