在视图层次结构上,需要澄清

时间:2011-11-21 00:08:01

标签: objective-c ios view

在此代码示例中,我尝试生成以下视图层次结构

  

窗口 - >背景图片 - >滚动视图 - >文字视图

然而我所看到的只是

  

窗口 - >背景图片

我错过了什么?

-(void) viewWillAppear:(BOOL)animated {

    UIScrollView *scrollWindow = [[UIScrollView alloc] 
                 initWithFrame:CGRectMake(30, 30, 440, 212)];

    UITextView *scrollableText = [[UITextView alloc] init];

    [scrollableText setEditable:NO];
    [scrollableText setText:@"Why, hello there"];

    [scrollWindow addSubview:scrollableText];

    UIImage *backgroundImage = [[UIImage alloc] initWithCGImage:
             [UIImage imageNamed:@"about_bg.png"].CGImage];
    UIImageView *backgroundView = [[UIImageView alloc] 
             initWithImage:backgroundImage];

    [backgroundView addSubview:scrollWindow];

    [[self view] addSubview:backgroundView];

}

2 个答案:

答案 0 :(得分:2)

Andrew没有将滚动视图设置为后台UIImageView视图的子视图。但滚动视图 是不可见的。仅显示其内容(scrollableText)。你还没有设置scrollableText的框架,所以它实际上也是隐形的。初始如下:

[scrollableText setEditable:NO];
[scrollableText setText:@"Why, hello there"];
[scrollableText setFrame:CGRectMake(0, 0, 100, 100)];

你应该看到它。

答案 1 :(得分:0)

您需要的是这种层次结构:

Window
  background image view
  scroll view
    text view

尝试在窗口中添加两个子视图,而不是将它们全部作为子视图推送到彼此.''-

   - (void) viewWillAppear:(BOOL)animated {

      UIScrollView *scrollWindow = [[UIScrollView alloc] initWithFrame:CGRectMake(30, 30, 440, 212)];

      UITextView *scrollableText = [[UITextView alloc] init];

      UIImageView *backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"about_bg.png"]];

      scrollableText.editable = NO;
      scrollableText.text = @"Why, hello there";

      [scrollWindow addSubview:scrollableText];

      [self.view addSubview:backgroundView];
      [self.view addSubview:scrollWindow];
    }