将 UIScrollView 添加到 UIViewController [英] Adding UIScrollView to a UIViewController

查看:32
本文介绍了将 UIScrollView 添加到 UIViewController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 UIViewController,我想给它添加一个 UIScrollView(启用滚动支持),可以吗?

I have a UIViewController, and I want to add a UIScrollView to it (enable scroll support), is it possible?

我知道这是可能的,如果你有一个 UIScrollView 来添加一个 UIViewController 到它,但我也很感兴趣,如果 reverse 是真的,如果我可以添加一个 UIScrollView 到一个现有的 UIViewController,这样我就可以获得滚动功能.

I know it is possible, if you have a UIScrollView to add a UIViewController to it, but I'm interested also if reverse was true, if I cann add a UIScrollView to an existing UIViewController, such that I get scrolling feature.

编辑

我想我找到了答案:将 UIViewController 添加到 UIScrollView

I think I have found an answer: Adding a UIViewController to UIScrollView

推荐答案

UIViewController 有一个 view 属性.因此,您可以将 UIScrollView 添加到其 view.换句话说,您可以将滚动视图添加到视图层次结构中.

An UIViewController has a view property. So, you can add a UIScrollView to its view. In other words, you can add the scroll view to the view hierarchy.

这可以通过代码或通过XIB来实现.此外,您可以将视图控制器注册为滚动视图的代理.通过这种方式,您可以实现用于执行不同功能的方法.参见 UIScrollViewDelegate 协议.

This is can achieved by code or through XIB. In addition, you can register the view controller as the delegate for your scroll view. In this way, you can implement methods for performing different functionalities. See UIScrollViewDelegate protocol.

// create the scroll view, for example in viewDidLoad method
// and add it as a subview for the controller view

[self.view addSubview:yourScrollView];

您还可以覆盖 UIViewController 类的 loadView 方法,并将滚动视图设置为您正在考虑的控制器的主视图.

You could also override loadView method for UIViewController class and set the scroll view as the main view for the controller you are considering.

编辑

我为您创建了一个小样本.在这里,您有一个滚动视图作为 UIViewController 视图的子视图.滚动视图有两个子视图:view1(蓝色)和 view2(绿色).

I created a little sample for you. Here, you have a scroll view as a child of the view of a UIViewController. The scroll view has two views as children: view1 (blue color) and view2 (green color).

在这里,我想您只能向一个方向滚动:水平或垂直.在下面,如果水平滚动,您可以看到滚动视图按预期工作.

Here, I suppose you can scroll in only one direction: horizontally or vertically. In the following, if you scroll horizontally, you can see that the scroll view works as expected.

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIScrollView* scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
    scrollView.backgroundColor = [UIColor redColor];
    scrollView.scrollEnabled = YES;
    scrollView.pagingEnabled = YES;
    scrollView.showsVerticalScrollIndicator = YES;
    scrollView.showsHorizontalScrollIndicator = YES;
    scrollView.contentSize = CGSizeMake(self.view.bounds.size.width * 2, self.view.bounds.size.height);    
    [self.view addSubview:scrollView];

    float width = 50;
    float height = 50;
    float xPos = 10;
    float yPos = 10;

    UIView* view1 = [[UIView alloc] initWithFrame:CGRectMake(xPos, yPos, width, height)];
    view1.backgroundColor = [UIColor blueColor];    
    [scrollView addSubview:view1];

    UIView* view2 = [[UIView alloc] initWithFrame:CGRectMake(self.view.bounds.size.width + xPos, yPos, width, height)];
    view2.backgroundColor = [UIColor greenColor];    
    [scrollView addSubview:view2];
}

如果您只需要垂直滚动,您可以进行如下更改:

If you need to scroll only vertically you can change as follows:

scrollView.contentSize = CGSizeMake(self.view.bounds.size.width, self.view.bounds.size.height * 2);

显然,你需要重新排列view1view2的位置.

Obviously, you need to rearrange the position of view1 and view2.

附言在这里,我使用的是 ARC.如果不使用ARC,则需要显式release alloc-init 对象.

P.S. Here I'm using ARC. If you don't use ARC, you need to explicitly release alloc-init objects.

这篇关于将 UIScrollView 添加到 UIViewController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆