Objective c 如何在UIScrollView(Obj-C)中禁用垂直滚动
如果可能,我想从UIScrollView禁用垂直滚动。。我的代码如下。。工作正常,但用户可以上下滚动,我相信这不应该存在。。提前谢谢Objective c 如何在UIScrollView(Obj-C)中禁用垂直滚动,objective-c,ios,uiscrollview,Objective C,Ios,Uiscrollview,如果可能,我想从UIScrollView禁用垂直滚动。。我的代码如下。。工作正常,但用户可以上下滚动,我相信这不应该存在。。提前谢谢 UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 100, self.view.frame.size.width, self.view.frame.size.height / 3)]; scroll.contentSize = CGSizeMake
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];
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.paginEnabled=是;
scroll.backgroundColor=[UIColor blackColor];
int xVal=30;
NSInteger numberOfViews=5;
对于(int i=0;i
此处可能存在重复项
或者您也可以尝试以下方法:
self.scrollview.contentSize = CGSizeMake(self.scrollview.frame.size.width * number_of_items, 1);
必须将scrollview内容高度设置为scroll view高度
CGSize scrollableSize=CGSizeMake(scrollableWidth,您的ScrollViewHeight);
[myScrollView setContentSize:scrollableSize];
假设它是一个iPhone应用程序,那么屏幕分辨率是320×480
现在,您将滚动视图的高度设置为self.view.frame.size.height/3
。
在这里,视图的高度实际上是460,而不是480(Status bar为20px)
因此,当您将另一个视图作为子视图添加到您的滚动视图时,其框架将从滚动的内容视图中消失。因此,您需要在设置帧/内容大小时对其进行管理
让我知道这是否适用于您。只要更改UIScrollView的contentSize就可以了。增加其宽度大小和高度应与当前相同。此外,您还可以隐藏垂直滚动条
scroll.showsVerticalScrollIndicator = NO;
scroll.contentSize = CGSizeMake(scroll.contentSize.width + xVal,scroll.frame.size.height);
你应该这样做:
aScrollView.scrollsToTop = NO;
aScrollView.delegate = self;
aScrollView.contentSize = CGSizeMake(aScrollView.frame.size.width * X, aScrollView.frame.size.height/2);
在我的情况下,我无法获得scrollview的高度(由于autolayout,我无法在viewDidLoad中获得高度)。您可以将其添加到委托方法中
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
self.scrollView.contentOffset = CGPointMake(self.scrollView.contentOffset.x, 0);
}
在xml文件中,有两个属性可用于scrollview:水平滚动和垂直滚动。根据您的要求,您可以勾选或取消勾选,如果您想停止垂直或水平滚动,则必须使scrollview的内容大小分别与scrollview的高度或宽度相同@NayanChauhan它被标记为ios。我把你的代码放在一个新项目的viewDidLoad中,但我根本没有卷轴。。。请尝试在此方法末尾设置scrollView contentSize。只需设置
contentSize
。相信我,这是有效的。它不起作用的唯一原因是,如果您像这样设置contentSizeCGSizeMake(yourdiredWidth,0)//这是正确的
,然后将其contentSize设置为大于scrollView框架高度的高度。对我有用,谢谢。在iOS 8.1.1上使用TableViewCell中的scrollView时效果完美!谢谢
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
self.scrollView.contentOffset = CGPointMake(self.scrollView.contentOffset.x, 0);
}