2016-08-02 125 views
0

我想添加約束條件到UIScrollView的標籤(子視圖的滾動視圖) 但滾動視圖不會滾動和拖尾永不工作。將約束添加到ScrollView

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]]; 

回答

0

如果您有這行self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width, self.scrollView.frame.size.height);然後滾動視圖從來不滾動,因爲在所有內容的大小是一樣的滾輪架,內容大小的scrollView必須大於以滾動視圖幀更大滾動

嘗試這樣的事情

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

那麼你必須滾動視圖滾動

我希望這可以幫助你

+0

謝謝,我試了一下shinetext的拖尾約束不起作用。 – user1688346

+0

試試這個'self.scrollView.contentSize = CGSizeMake(self。scrollView.frame.size.width,self.shineText.frame.size.height);' –

0

一般來說,UIScrollView和經銷商的佈局工作時,我們需要添加滾動視圖與相同尺寸的滾動視圖一個UIView子視圖。


Here is tutorial其中詳細解釋了這一點。
但是本教程適用於使用.nib文件中的滾動視圖。

要添加滾動視圖以編程方式,請通過Apple developer technical note
它表明:

使用約束滾動視圖中鋪陳子視圖,爲確保限制配合滾動視圖的所有四個邊緣,不靠滾動視圖來獲取它們的大小。

0

試試這種棘手的方式來處理故事板中的滾動視圖。我創建了一個小視頻教程來展示scroll view的工作方式。你應該有一個小的知識autolayout

step 1change your viewcontroller size as your need

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

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

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

step 5create an outlet to the child view width to your controller and give the width programatically(the device width)

這個小教程演示如何做到這一點..

part one : less thant 5 min

part two : less than 5 min

希望這將有助於你。