且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何将滚动视图添加到我的iOS应用程序?

更新时间:2023-01-26 10:27:19

您可以通过编程方式设置内容视图(滚动区域)尺寸:

You can set the content view (scrolling area) dimensions programmatically:

[scrollView setContentSize:CGSizeMake(contentViewWidth, contentViewHeight)];

然后使用内容视图的坐标系将视图组件添加到滚动视图.假设您的滚动视图是100x100像素,并且您希望滚动视图的宽度是原来的两倍:

And then add you view components to the scroll view using the coordinate system of the content view. So say your scroll view was 100x100 pixels and you wanted the scroll view to be twice as wide:

[scrollView setContentSize:CGSizeMake(200.0f, 100.0f)];

然后您可以添加一个按钮以显示在滚动视图的每一半上:

You could then add a button to appear on each half of the scroll view:

// First half of content view
button1.frame.origin.x = 10.0f;
button1.frame.origin.y = 50.0f;
[scrollView addSubView:button1];

// First half of content view
button2.frame.origin.x = 110.0f; // NOTE that 110 is outside of
button2.frame.origin.y = 50.0f;  // the scroll view frame
[scrollView addSubView:button2];

您可以可能将滚动视图的每个页面设计为NIB中的单独子视图,然后在Interface Builder中或以编程方式将子视图的原点移动到正确的页面位置-但我从未尝试过.如果这行得通,那么您就可以使用IB来布局按钮.

You could probably design each page of the scroll view as a separate sub view in the NIB and then shift the sub views origin to the correct page location either in Interface Builder or programmatically - but I have never tried this. If this works you'd then be able to layout your buttons using IB.

我想知道这是否有效.