带有autolayout的UIScrollView SetContentSize

时间:2016-04-15 09:29:10

标签: ios objective-c iphone uiscrollview autolayout

我正在故事板中使用UIScrollview并在其中应用autolayout。在UIScrollview中,我正在插入contentview作为UIScrollview的autolayout需要一个内容视图。现在,根据我的应用场景,我必须更改contentView高度,因此我以编程方式更改了contentView的自动布局。

现在我以编程方式更改autolayout后得到 contentView的height = 445 。以下是我的代码

//----set height of content view----//
CGRect contentViewFrame = contentView.frame;
contentViewFrame.size.height= viewMainContent.frame.origin.y+viewMainContent.frame.size.height + 20 ;
contentView.frame = contentViewFrame;
_contentHtConstraint.constant = contentView.frame.size.height;

取决于此 UIScrollview的高度也是445 。但是UIScrollview的 contentSize将是675

有人能说我如何管理contentSize吗?

1 个答案:

答案 0 :(得分:0)

为内容视图添加高度约束。并为它设置出口。 如果您希望在视图控制器启动时更改内容高度。在viewWillLayoutSubviews中设置它

- (void) viewWillLayoutSubviews{
        [super viewWillLayoutSubviews];

        //Update your content view height, by updating constant size of height constraint
       self.contentViewHeightConstraint.constant = 1000;//set this value as per your need. 

       // update your view constraint if needed
       [self.view updateConstraintsIfNeeded];

      //Set content size for your scrollview 
      [self.scrollView setContentSize:CGSizeMake(self.scrollView.contentSize.width, self.contentViewHeightConstraint.constant)];

}