关于视图层次结构,需要澄清

发布于 2024-12-17 01:22:00 字数 882 浏览 2 评论 0原文

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

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

我所看到的只是

窗口->背景图片

请问我缺少什么?

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

}

In this code example, i am trying to produce the following view hierarchy

Window -> background image -> scroll view -> text view

All i see however is

Window -> background image

What am i missing please?

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

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

唯憾梦倾城 2024-12-24 01:22:01

安德鲁关于不让滚动视图成为背景 UIImageView 视图的子视图是正确的。但滚动视图是不可见的。仅显示其内容 (scrollableText)。而且您还没有设置 scrollableText 的框架,因此它实际上也是不可见的。像这样初始化:

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

你应该看到它。

Andrew is right about not making the scroll view a subview of the background UIImageView view. But the scroll view is invisible. Only its content (scrollableText) will show. And you haven't set scrollableText's frame, so it's effectively invisible too. Init like so:

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

And you should see it.

‖放下 2024-12-24 01:22:01

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

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

What you need is this hierarchy:

Window
  background image view
  scroll view
    text view

Try adding two subviews to your window, not shove them all as subviews into each other.''-

   - (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];
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文