【发布时间】:2013-12-19 14:30:45
【问题描述】:
我有一个名为MenuViewController 的UIViewController,我在其中创建了一个UIView MessageView 并将其传递给NSString:
MessageView *mess = [[MessageView alloc]initWithFrame:CGRectMake(0, 0, 1024, 768)]; [mess messageString:@"A:text\nM:text\nA:text\nM:text"]; [self.view addSubview:mess];
在MessageView 中,我正在创建一个UIScrollView 和几个UILabels;我的问题如下,我无法理解行为:
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(540, 50, 360, 160)];
scrollView.backgroundColor = [UIColor redColor];
[self addSubview:scrollView];
}
return self;
}
- (void)messageString:(NSString *)string
{
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(scrollView.frame.origin.x, scrollView.frame.origin.y, scrollView.frame.size.width, scrollView.frame.size.height/6)];
>>>>> HERE is the problem <<<<<<
if i try to add label to scrollView it does not appear
[scrollView addSubview:label];
if i try to add label to self it appears
[self addSubview:label];
}
我想在 MessageView 中创建我想在此处显示的所有内容,然后从视图控制器中调用它。有人知道可能是什么问题吗?
【问题讨论】:
-
当你设置一个视图的框架时,它应该在superview的坐标系中。因此,如果您将标签添加为滚动视图的子视图,则标签 init 方法应在标签的框架计算中使用 scrollView.bounds,而不是 scrollView.frame。还有一点需要注意:最好将 messageString 重命名为 setMessageString,因为以前的样式用于 getter,而不是 setter。
-
你能看到你的滚动视图吗?我在你的问题中看到滚动视图的 x 位置是 540
标签: ios uiview uiviewcontroller uiscrollview