ios 将 UIScrollView 添加到 UIViewController

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14055950/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 21:38:38  来源:igfitidea点击:

Adding UIScrollView to a UIViewController

iosuiviewcontrolleruiscrollview

提问by user1903992

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

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

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

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

Edit

编辑

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

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

回答by Lorenzo B

An UIViewControllerhas a viewproperty. So, you can add a UIScrollViewto its view. In other words, you can add the scroll view to the view hierarchy.

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

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 UIScrollViewDelegateprotocol.

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

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

[self.view addSubview:yourScrollView];

You could also override loadViewmethod for UIViewControllerclass and set the scroll view as the main view for the controller you are considering.

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

Edit

编辑

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).

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

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);

Obviously, you need to rearrange the position of view1and view2.

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

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

PS 这里我使用的是 ARC。如果不使用 ARC,则需要显式地分配release初始化对象。