【发布时间】:2012-08-04 09:13:23
【问题描述】:
如果可能的话,我想从我的 UIScrollView 禁用垂直滚动。我的代码如下所示。工作正常,除了用户可以上下滚动,我相信这不应该存在。提前致谢..
UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 100, self.view.frame.size.width, self.view.frame.size.height / 3)];
scroll.contentSize = CGSizeMake(scroll.contentSize.width,scroll.frame.size.height);
scroll.pagingEnabled = YES;
scroll.backgroundColor = [UIColor blackColor];
int xVal = 30;
NSInteger numberOfViews = 5;
for (int i = 0; i < numberOfViews; i++) {
UILabel *testLabel1 = [[UILabel alloc] initWithFrame:CGRectMake(xVal, 0, 90, 100)];
UILabel *testLabel2 = [[UILabel alloc] initWithFrame:CGRectMake(xVal, 20, 90, 100)];
UILabel *testLabel3 = [[UILabel alloc] initWithFrame:CGRectMake(xVal, 40, 90, 100)];
testLabel2.backgroundColor = [UIColor clearColor];
testLabel2.text =@"Test1";
testLabel2.textColor = [UIColor whiteColor];
testLabel2.font = [UIFont boldSystemFontOfSize:12];
testLabel1.backgroundColor = [UIColor clearColor];
testLabel1.text =@"Test2";
testLabel1.textColor = [UIColor whiteColor];
testLabel1.font = [UIFont boldSystemFontOfSize:12];
testLabel3.backgroundColor = [UIColor clearColor];
testLabel3.text =@"Test3";
testLabel3.textColor = [UIColor whiteColor];
testLabel3.font = [UIFont boldSystemFontOfSize:12];
xVal += 120;
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(xVal, 30, 150, 130)];
view.backgroundColor = [UIColor blackColor];
xVal += 200;
[scroll addSubview:testLabel1];
[scroll addSubview:testLabel2];
[scroll addSubview:testLabel3];
[scroll addSubview:view];
}
[self.view addSubview:scroll];
【问题讨论】:
-
是的,我用过同样的东西,但对我没用!!
-
@NayanChauhan 它被标记为 ios。
-
我把你的代码放在一个新项目的 viewDidLoad 中,我根本没有滚动...尝试在这个方法的末尾设置 scrollView contentSize。
-
只需设置
contentSize。相信我,它有效。它不起作用的唯一原因是如果你像这样设置 contentSizeCGSizeMake(yourDesiredWidth , 0 ) ; //which is correct然后将它的 contentSize 设置为高度大于 scrollView 的框架高度。
标签: objective-c ios uiscrollview