UIScrollview不滚动

时间:2014-05-16 20:10:25

标签: ios objective-c uiscrollview contentsize

我知道有几十个关于scrollview的类似问题,但我已经尝试了所有内容,Scrollview不滚动。

首先,我创建了一个带有xib文件的uiviewcontroller,并将其设置为自由形式并排列它的大小,并在视图上添加了一个滚动视图。

enter image description here

然后将其作为childviewcontroller添加到另一个视图控制器。

-(void)scrollViewSetup
{
    if (!_scrollView) {
        _scrollView=[[UIView alloc] initWithFrame:CGRectMake(0,200,self.scrollView.frame.size.width,200)];
        [self.view addSubview:_scrollView];

        ScrollView *displayPortf=[[ScrollView alloc] init];
        //add as a child view controller here
        //displayPortf.delegate=(id)self;
        [_scrollView addSubview:displayPortf.view];
        [displayPortf didMoveToParentViewController:self];
        [self addChildViewController:displayPortf];
    }
}

iPad上滚动视图的内容大小为(768,200)。 我尝试将content size设置为(1920,200)viewDidLoad没有工作,然后viewDidAppear没有工作,viewDidLayoutSubviews没有工作

然后我检查了use auto layout框并尝试通过editor->resolve auto layout issues->add constraints

添加常量

如何让scrollview滚动?

注意:父视图使用自动布局和故事板。

1 个答案:

答案 0 :(得分:1)

我猜问题的原因是scrollView的宽度为零 当你这样称呼时:

_scrollView=[[UIView alloc] initWithFrame:CGRectMake(0,200,self.scrollView.frame.size.width,200)];

_scrollView以宽度self.scrollView.frame.size.width启动,但此时self.scrollView为零,因为它与尚未启动的实例变量_scrollView相关。

尝试为视图宽度设置一些非零浮点值:
_scrollView=[[UIView alloc] initWithFrame:CGRectMake(0,200,768,200)];