将约束添加到ScrollView

时间:2016-08-02 16:18:19

标签: ios objective-c

我正在尝试使用标签(scrollview的子视图)向UIScrollView添加约束 但是滚动视图不会滚动,拖尾也不会工作。

    float y = self.navigationController.navigationBar.frame.size.height + 30;
self.scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0,0, 0,0)];
[self.scrollView setBackgroundColor:[UIColor blueColor]];
[self.scrollView setScrollEnabled:YES];
[transparentImageView addSubview:self.scrollView];


self.scrollView.translatesAutoresizingMaskIntoConstraints = NO;
[transparentImageView addConstraint:[NSLayoutConstraint constraintWithItem:self.scrollView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:transparentImageView attribute:NSLayoutAttributeBottom multiplier:1.0f constant:-120.0]];
//leading
[transparentImageView addConstraint:[NSLayoutConstraint constraintWithItem:self.scrollView attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:transparentImageView attribute:NSLayoutAttributeLeading multiplier:1.0f constant:20.0f]];
//trailing
[transparentImageView addConstraint:[NSLayoutConstraint constraintWithItem:self.scrollView attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:transparentImageView attribute:NSLayoutAttributeTrailing multiplier:1.0f constant:-20.0]];
[transparentImageView addConstraint:[NSLayoutConstraint constraintWithItem:self.scrollView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:transparentImageView attribute:NSLayoutAttributeTop multiplier:1.0f constant:y]];

self.shineText = [[RQShineLabel alloc]initWithFrame:CGRectMake(0,0, 0, 0)];
[self setupText];
[self.shineText setBackgroundColor:[UIColor redColor]];
[self.scrollView addSubview:self.shineText];


self.shineText.translatesAutoresizingMaskIntoConstraints = NO;
self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width, self.scrollView.frame.size.height);

//bottom
[self.scrollView addConstraint:[NSLayoutConstraint constraintWithItem:self.shineText attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.scrollView attribute:NSLayoutAttributeBottom multiplier:1.0f constant:-10.0f]];
//leading
[self.scrollView addConstraint:[NSLayoutConstraint constraintWithItem:self.shineText attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.scrollView attribute:NSLayoutAttributeLeading multiplier:1.0f constant:10.0f]];
//trailing
[self.scrollView addConstraint:[NSLayoutConstraint constraintWithItem:self.shineText attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self.scrollView attribute:NSLayoutAttributeTrailing multiplier:1.0f constant:-500.0f]];
[self.scrollView addConstraint:[NSLayoutConstraint constraintWithItem:self.shineText attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.scrollView attribute:NSLayoutAttributeTop multiplier:1.0f constant:20]];

3 个答案:

答案 0 :(得分:0)

如果您有这一行self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width, self.scrollView.frame.size.height);,那么您的滚动视图根本不会滚动,因为您的内容大小与滚动条框架相同,scrollView的内容大小必须大于scrollView框架以滚动

尝试使用类似的东西

self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width, 1000);

然后你的scrollView必须滚动

我希望这有助于你

答案 1 :(得分:0)

通常,在使用UIScrollView和自动布局时,我们需要在滚动视图上添加一个UIView作为子视图,其大小与滚动视图相同。

Here is tutorial详细解释了这一点 但本教程适用于.nib文件中的滚动视图。

要以编程方式添加滚动视图,请浏览Apple developer technical note 它表明:

  

使用约束在滚动视图中布置子视图,确保约束与滚动视图的所有四个边相关联,并且不依赖滚动视图来获取它们的大小。

答案 2 :(得分:0)

尝试这种棘手的方式在storyboard中使用scrollview。我创建了一个小视频教程,以展示使用SQLCMD -b -S"MyServer" -d"MyDatabase" -Q"IF NOT EXISTS(SELECT * FROM MyTable) RAISERROR('Fail',16,1)" 的方便程度。您应该对scroll view知之甚少。

autolayoutstep 1

change your viewcontroller size as your needstep 2

add a scroll view and setup constarints (top, lead, trail and top) to the conatainer view.step 3

then add another view on to scroll view(this is your child view where you add your all other IBOutlets)step 4

then add constraints to it. (top, lead, trail and top to scroll view) + (width and height to it self)step 5

这个小教程向您展示了如何做到这一点..

part one : less thant 5 min

part two : less than 5 min

希望这会对你有所帮助。