如何动态设置 UIScrollView 的内容大小 [英] How to set content size of UIScrollView dynamically

查看:21
本文介绍了如何动态设置 UIScrollView 的内容大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有关于 UIScrollview 的问题.

故事是我有一个名为 ChartsView 的 UIView,我通过覆盖方法 drawRect() 自己重新绘制它.绘图内容是动态生成的.所以直到运行时我才知道它的大小.问题是我如何/在哪里可以动态设置其超级视图 (scrollView) 的内容大小?对此有任何想法吗?

The story is I have a UIView named ChartsView which I re-draw it myself by override method drawRect(). The content of drawing was generated dynamically. So I do not know its size until runtime. The question is how/where can I set its super view's (scrollView) content size dynamically?Any idea about that?

    - (void)viewDidLoad
    {
        [super viewDidLoad];

// not this way. it's fixed size.....
        ChartsView *chartsView = [[ChartsView alloc]initWithFrame:CGRectMake(0, 0, 320, 800)]; 

        self.scrollView.contentSize = chartsView.frame.size;

        [self.scrollView addSubview:chartsView];

    }

推荐答案

更简单的方法

- (void)viewDidLoad
{
    float sizeOfContent = 0;
    UIView *lLast = [scrollView.subviews lastObject];
    NSInteger wd = lLast.frame.origin.y;
    NSInteger ht = lLast.frame.size.height;

    sizeOfContent = wd+ht;

    scrollView.contentSize = CGSizeMake(scrollView.frame.size.width, sizeOfContent);
}

这篇关于如何动态设置 UIScrollView 的内容大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆