关于视图层次结构,需要澄清
在此代码示例中,我尝试生成以下视图层次结构
窗口 ->背景图片->滚动视图->文本视图
我所看到的只是
窗口->背景图片
请问我缺少什么?
-(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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
安德鲁关于不让滚动视图成为背景
UIImageView
视图的子视图是正确的。但滚动视图是不可见的。仅显示其内容 (scrollableText
)。而且您还没有设置scrollableText
的框架,因此它实际上也是不可见的。像这样初始化:你应该看到它。
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 setscrollableText
's frame, so it's effectively invisible too. Init like so:And you should see it.
您需要的是这样的层次结构:
尝试将两个子视图添加到您的窗口中,而不是将它们全部作为子视图相互推入。''-
What you need is this hierarchy:
Try adding two subviews to your window, not shove them all as subviews into each other.''-