简体   繁体   English

UIScrollView不垂直滚动

[英]UIScrollView doesn't scroll vertically

I made iPhone app in which I have this hierarchy, 我制作了iPhone应用程序,我有这个层次结构,

: When I try to scroll my scrollview it doesn't scroll I am using Autolayout in my project :当我尝试滚动我的滚动视图时,它不滚动我在我的项目中使用Autolayout

content size of my scrollview is greater then the height of my scrollview still it doesn't scroll. 我的scrollview的内容大小比我的scrollview的高度还要大,但它不会滚动。

Here is my code snippet wrote in viewDidLoad 这是我在viewDidLoad编写的代码片段

    UIView *tempo = (UIView *)[scrollVw viewWithTag:6];//View which is inside Scrollview

    float sizeOfContent = 0;
    for (int i = 0; i < [tempo.subviews count]; i++) {
        UIView *view =[tempo.subviews objectAtIndex:i];
        sizeOfContent += view.frame.size.height;

    }

    NSLog(@"sizeOfContent = %f", sizeOfContent);
    scrollVw.contentSize = CGSizeMake(scrollVw.frame.size.width, 1500);

    NSLog(@"scrollVw width = %f -- scrollVw height =%f",scrollVw.frame.size.width, scrollVw.frame.size.height);

    NSLog(@"tempo width = %f -- tempo height =%f",tempo.frame.size.width, tempo.frame.size.height);

Log Result: 记录结果:

sizeOfContent = 1500
scrollVw width = 320.000000      scrollVw height = 416.000000 
tempo width = 320.000000         tempo height =  416.000000

Where I am doing mistake? 我在哪里做错了?

If you are using autolayout, then your views are ultimately layed out after viewDidLoad. 如果您使用autolayout,那么您的视图最终会在viewDidLoad之后布局。 So move your code into viewWillAppear or viewDidLayoutSubviews and you should be fine. 所以将你的代码移动到viewWillAppear或viewDidLayoutSubviews,你应该没问题。

Please try to call this method after setting your content size 请在设置内容大小后尝试调用此方法

scrollVw.contentSize = CGSizeMake(scrollVw.frame.size.width, 1500);

[scrollVw layoutIfNeeded];

Where are you logging your contentSize etc? 你在哪里记录你的contentSize等? In viewDidLoad? 在viewDidLoad中?

What does it show in viewDidLayoutSubviews? 它在viewDidLayoutSubviews中显示了什么? I think your constraints are messing with your content size. 我认为你的约束正在弄乱你的内容大小。

hope this will help you 希望这个能对您有所帮助

, when you use a UIScrollView with auto layout the auto layout not works like you expect from it.(Your image showing a red arrow before going any where else please click that and adjust your auto layout ) ,当你使用带有自动布局的UIScrollView时,自动布局不会像你期望的那样工作。(你的图像显示红色箭头,然后去任何其他地方请点击它并调整你的自动布局)

View (main view of my UIViewController) – with
-Scroll View (UIScrollView)
--Container View 
----Content1 
----Content2 (etc)

-> The scroll view must have at least 1 content that attaches to each of its edges, eg top, left, right, and bottom(Container View ). - >滚动视图必须至少有1个附加到其每个边缘的内容,例如顶部,左侧,右侧和底部(容器视图)。

-> Do not fix the height, width of scroll view just give 4 constraint to it(leading, trailing, top, and bottom ). - >不要固定滚动视图的高度,宽度,只给它4个约束(前导,尾随,顶部和底部)。

->give Container view leading, trailing, top, and bottom spaces to the Scroll View. - >将容器视图前导,尾随,顶部和底部空格分配给滚动视图。

and give constraint to inner Content. 并限制内部内容。 then 然后

if(self.viewScroll.contentSize.height>self.viewScroll.frame.size.height)
{
    [self.viewScroll setScrollEnabled:YES];
}
else
{
    [self.viewScroll setScrollEnabled:NO];(if you want)
}

从viewcontroller取消选中autolayout,然后尝试并确保contentsize.height不仅仅是scrollview高度。

I think it's ok if your scrollview's height (568) is greater than the scrollview.contentsize(eg 350). 我认为滚动视图的高度(568)大于scrollview.contentsize(例如350)是可以的。 The only thing that helped me, is to change the contentmode of the Imageview that we put on the scrollview as: 唯一能帮助我的是改变我们在scrollview上放置的Imageview的内容模式:

imgView.contentMode = UIViewContentModeScaleToFill;

You don't even need to access the method scrollViewDidEndZooming this way. 您甚至不需要以这种方式访问​​方法scrollViewDidEndZooming。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM