AnUIViewController
にはview
プロパティがあります。したがって、にを追加できUIScrollView
ますview
。つまり、スクロールビューをビュー階層に追加できます。
これは、コードまたはXIBを介して実現できます。さらに、スクロールビューのデリゲートとしてViewControllerを登録できます。このようにして、さまざまな機能を実行するためのメソッドを実装できます。UIScrollViewDelegate
プロトコルを参照してください。
// create the scroll view, for example in viewDidLoad method
// and add it as a subview for the controller view
[self.view addSubview:yourScrollView];
loadView
クラスのメソッドをオーバーライドUIViewController
して、検討しているコントローラーのメインビューとしてスクロールビューを設定することもできます。
編集
私はあなたのために小さなサンプルを作成しました。ここでは、のビューの子としてスクロールビューがありますUIViewController
。スクロールビューには、子として2つのビューがあります:(view1
青色)とview2
(緑色)。
ここでは、水平方向または垂直方向の1方向にしかスクロールできないと思います。以下では、水平方向にスクロールすると、スクロールビューが期待どおりに機能していることがわかります。
- (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];
}
垂直方向にのみスクロールする必要がある場合は、次のように変更できます。
scrollView.contentSize = CGSizeMake(self.view.bounds.size.width, self.view.bounds.size.height * 2);
明らかに、との位置を再配置する必要がview1
ありview2
ます。
PSここではARCを使用しています。ARCを使用しない場合は、release
オブジェクトを明示的にalloc-initする必要があります。