将视图放在UIScrollview中

时间:2012-10-04 16:24:25

标签: objective-c ios gridview uiview

这就是我想要实现的目标。

|------------------------------------------------------|
|                     view 1 here                      |
|                                                      |
|                                                      |
|                                                      |
|                                                      |
|                                                      |
|                                                      |
|                                                      |
|                                                      |
|                                                      |
|                                                      |
|                                                      |
|                       view 2 here                    |
|                                                      |
|                                                      |
|                                                      |
|                                                      |
|                                                      |
|                                                      |
|                                                      |
|                    view 3 here                       |
|                                                      |
|                                                      |
|                                                      |
|                                                      |
|                                                      |
|                                                      |
|                                                      |
| -----------------------------------------------------|

我想要一个包含3个视图的滚动视图。我正在做以下事情。

-(void)viewDidLoad
{
    keeperView = [[UIView alloc] init];
    aanvallerView = [[UIView alloc] init];
    middenvelderView = [[UIView alloc] init];
    listView.contentSize=CGSizeMake(1280,2360);
    [listView addSubview:keeperView];
    [listView addSubview:aanvallerView];
    [listView addSubview:middenvelderView];
}

但它只显示一个视图。有谁知道问题是什么?

亲切的问候和提前谢谢。

2 个答案:

答案 0 :(得分:4)

可能是他们没有框架。致电initWithFrame

上的UIViews :.

答案 1 :(得分:2)

在viewDidLoadMethod中:

UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
[scrollView setContentSize:CGSizeMake(320, 1100)];
[scrollView setScrollEnabled:YES];
[self.view addSubview:scrollView];

UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(0, 50, 320, 300)];
[view1 setBackgroundColor:[UIColor redColor]];
[scrollView addSubview:view1];

UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(0, 400, 320, 300)];
[view2 setBackgroundColor:[UIColor blueColor]];
[scrollView addSubview:view2];

UIView *view3 = [[UIView alloc] initWithFrame:CGRectMake(0, 750, 320, 300)];
[view3 setBackgroundColor:[UIColor yellowColor]];
[scrollView addSubview:view3];

结果:

Different View

Different views 2

Different Views 3

所有3个观看次数都在Scrollview上! :)

通过使用addSubview,您现在可以将所有想要的内容放在视图上;) 您甚至可以使用scrollview ^^

制作另一个滚动视图