Resize content view in UIScrollView no working

放肆的年华 提交于 2020-01-17 03:01:27

问题


I have this ViewController :

Main View
_-Scroll
__- content view

I need to resize dynamically the content view

I have this method resizeScrollView(), this is my method :

private func resizeScrollView(){
     self.mViewContainer.frame.size.height = 800
    self.mScrollView.contentSize = self.mViewContainer.frame.size
    NSLog("Scroll Height : " + NSNumber(float: Float(self.mScrollView.contentSize.height)).stringValue)
    NSLog("Container Height : " + NSNumber(float: Float(self.mViewContainer.frame.height)).stringValue)

}

and i have these logs :

2015-04-21 15:11:44.854 quotle[3673:60b] Scroll Height : 800
2015-04-21 15:11:44.856 quotle[3673:60b] Container Height : 800

At this step, both views are correctly resized.

On my scrolling event, i have link this method :

func scrollViewDidScroll(scrollView: UIScrollView) {
    NSLog("Scroll Height : " + NSNumber(float: Float(self.mScrollView.contentSize.height)).stringValue)
    NSLog("Container Height : " + NSNumber(float: Float(self.mViewContainer.frame.height)).stringValue)
}

And when i'm scrolling, i see these logs :

2015-04-21 15:11:43.556 quotle[3673:60b] Scroll Height : 800
2015-04-21 15:11:43.559 quotle[3673:60b] Container Height : 416

So, when i am starting to scrolling, my content view is resized...

My question is : What i have to do to keep my content view's height to 800 when i am scrolling and not have this autoresizing of my content view?

You can respond with Obj-C, i'll adapt it to swift.


回答1:


try this

- (void) fitForScroll:(UIView*) containerViewInScroll :(UIScrollView*)scrollView
{
    float w=0;
    float h=0;
    for (UIView *v in [containerViewInScroll subviews]) {
        float fw = v.frame.origin.x + v.frame.size.width;
        float fh = v.frame.origin.y + v.frame.size.height;
        w = MAX(fw, w);
        h = MAX(fh, h);
    }
    [containerViewInScroll setFrame:CGRectMake(containerViewInScroll.frame.origin.x,containerViewInScroll.frame.origin.y, w, h + 20)];
    [scrollView setContentSize:containerViewInScroll.frame.size];
}


----------


来源:https://stackoverflow.com/questions/29773504/resize-content-view-in-uiscrollview-no-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!