iOS滚动视图无法正常工作。

3

我在1000像素高处有一个按钮,需要使用uiscrollview才能访问该按钮。我实现了下面的scrollview代码并显示了scroll view,但无法向下滚动。我可能缺少一个重要的函数。如有任何提示或建议,将不胜感激。

 UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 680)];
scrollView.contentSize = CGSizeMake(320, 1000);
scrollView.clipsToBounds = YES;
super.view.backgroundColor = [UIColor whiteColor];
[scrollView setUserInteractionEnabled:YES];
[[self view] addSubview:scrollView];

你在哪里运行这段代码?是在 viewDidLoad 中吗? - Leon Storey
是的,我正在使用自动布局和viewdidload。 - Two Face
@TwoFace 请尝试关闭自动布局。 - Abdullah Shafique
3
你是在iPhone或iPad上运行吗?如果是在iPhone上,它肯定无法滚动到顶部。scrollView.frame.size.height不应超过其parentView的高度。 - Nitin Alabur
1
尝试在viewDidLayoutSubviews中设置contentSize,而不是在viewDidLoad:中设置。 - Steph Sharp
显示剩余2条评论
1个回答

0
问题在于您的按钮y坐标为1000像素,正如您所述。而scrollview内容的y坐标最多也只能达到1000像素。因此,scrollview只能滚动到按钮开始的位置。
UIButton *btn =[UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(50, 1000, 100,45);
[scrollView addSubview:btn];

像这样设置scrollview的内容大小

scrollView.contentSize = CGSizeMake(320, 1100);

这肯定会解决它。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接