Ios UIView-won';无法识别子视图

Ios UIView-won';无法识别子视图,ios,objective-c,cocoa-touch,Ios,Objective C,Cocoa Touch,我需要以编程方式创建带有UILabel子视图的UIView,因为它在动画方面比IB更有效。但我无法让UIView识别UILabel。以下是我在viewDidLoad中的代码: UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(764, 94, 200, 100)]; [label setText:@"Hello"]; [label setTextColor:[UIColor redColor]]; [label setBackgr

我需要以编程方式创建带有UILabel子视图的UIView,因为它在动画方面比IB更有效。但我无法让UIView识别UILabel。以下是我在viewDidLoad中的代码:

UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(764, 94, 200, 100)];
[label setText:@"Hello"];
[label setTextColor:[UIColor redColor]];
[label setBackgroundColor:[UIColor clearColor]];

UIView *hintView = [[UIView alloc]initWithFrame:CGRectMake(764, 94, 240, 198)];
[hintView setBackgroundColor:[UIColor colorWithRed:(34/255.f) green:(59/255.f) blue:(27/255.f) alpha:(255/255.f)]];

[hintView addSubview:label];
[self.view addSubview:hintView];

NSLog(@"subview %@", hintView.subviews);
谢谢

您正在设置宽边框,请尝试此选项

 UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 200, 100)];
您正在设置宽边框,请尝试此选项

 UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 200, 100)];

标签.frame.origin.x
(0)更改为(320-withOfYourLabel)

上述代码与自定义的
ui视图
(设置
X
)相同

如果您想获得自定义视图的子视图,请按照

for(UIView *subView in hintViewsubviews)
{
     /// here you got all subview of hintView

     if([subView isKindOfClass:[UILabel class]])
     {
            // here you can also check that subView is UILabel or not ?
     }
}

标签.frame.origin.x
(0)更改为(320-withOfYourLabel)

上述代码与自定义的
ui视图
(设置
X
)相同

如果您想获得自定义视图的子视图,请按照

for(UIView *subView in hintViewsubviews)
{
     /// here you got all subview of hintView

     if([subView isKindOfClass:[UILabel class]])
     {
            // here you can also check that subView is UILabel or not ?
     }
}

您使用的UIView和Label.bcz的帧大小相同,因此无法获得正确的输出。请参阅我对您的代码所做的更改,然后尝试此操作

UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(hintView.frame.origin.x+15, hintView.frame.origin.y+50, hintView.frame.size.width-60, hintView.frame.size.height-100)];
[label setText:@"Hello"];
[label setTextColor:[UIColor redColor]];
[label setBackgroundColor:[UIColor clearColor]];

UIView *hintView = [[UIView alloc]initWithFrame:CGRectMake(764, 94, 240, 198)];
[hintView setBackgroundColor:[UIColor colorWithRed:(34/255.f) green:(59/255.f)    blue:(27/255.f) alpha:(255/255.f)]];

[hintView addSubview:label];
[self.view addSubview:hintView];

NSLog(@"subview %@", hintView.subviews);

您使用的UIView和Label.bcz的帧大小相同,因此无法获得正确的输出。请参阅我对您的代码所做的更改,然后尝试此操作

UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(hintView.frame.origin.x+15, hintView.frame.origin.y+50, hintView.frame.size.width-60, hintView.frame.size.height-100)];
[label setText:@"Hello"];
[label setTextColor:[UIColor redColor]];
[label setBackgroundColor:[UIColor clearColor]];

UIView *hintView = [[UIView alloc]initWithFrame:CGRectMake(764, 94, 240, 198)];
[hintView setBackgroundColor:[UIColor colorWithRed:(34/255.f) green:(59/255.f)    blue:(27/255.f) alpha:(255/255.f)]];

[hintView addSubview:label];
[self.view addSubview:hintView];

NSLog(@"subview %@", hintView.subviews);

你是说你的日志没有显示标签吗?我复制并粘贴了您的代码,这对我来说很有效。我对您的问题有点怀疑标签是否不出现在视图上“我需要以编程方式创建一个带有UILabel子视图的UIView,因为它在动画方面比IB更有效。”请注意这样的注释,它们非常主观,可能不相关。这甚至让我怀疑一两件事:你也有很多绝对的硬编码点和尺寸,这通常会导致脆弱的工作。任何支持iPhone5的人如果依赖于iPhone4的屏幕尺寸,或者任何只支持肖像的人,或者任何曾经为Android开发过的人,都可以告诉你他们的痛苦。试着做一些相对的陈述。看起来你在hintView的x值视图之外创建标签。尝试在0-763范围内设置标签帧的第一个参数。不确定这是否能解决问题,但如果执行此当前代码,则标签不应显示在hintview中。您是说您的日志没有显示标签吗?我复制并粘贴了您的代码,这对我来说很有效。我对您的问题有点怀疑标签是否不出现在视图上“我需要以编程方式创建一个带有UILabel子视图的UIView,因为它在动画方面比IB更有效。”请注意这样的注释,它们非常主观,可能不相关。这甚至让我怀疑一两件事:你也有很多绝对的硬编码点和尺寸,这通常会导致脆弱的工作。任何支持iPhone5的人如果依赖于iPhone4的屏幕尺寸,或者任何只支持肖像的人,或者任何曾经为Android开发过的人,都可以告诉你他们的痛苦。试着做一些相对的陈述。看起来你在hintView的x值视图之外创建标签。尝试在0-763范围内设置标签帧的第一个参数。不确定这是否会解决问题,但如果执行此当前代码,则标签不应显示在hintview中。这将在声明(第5行)之前给出编译时错误,因为您正在使用
hintview
(第1行)。此外,在superview中添加子视图时,子视图坐标系从(0,0)开始。所以你需要说
UILabel*label=[[UILabel alloc]initWithFrame:CGRectMake(15,50,hintView.frame.size.width-60,hintView.frame.size.height-100)]。当您在声明(第5行)之前使用
hintView
(第1行)时,这将给出编译时错误。此外,在superview中添加子视图时,子视图坐标系从(0,0)开始。所以你需要说
UILabel*label=[[UILabel alloc]initWithFrame:CGRectMake(15,50,hintView.frame.size.width-60,hintView.frame.size.height-100)]