繁体   English   中英

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

[英]Adding a subview to another subview is not working

我试图在此处将标签(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];

是否显示introPadTutorial视图? 您的initWithFrame调用宽度为负数。

可能是你...

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

不要使用负宽度更改位置,而要使用原点更改位置。

用这个:

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM