简体   繁体   English

iPhone iOs 5,根据内容高度不起作用动态更改滚动查看器的高度吗?

[英]iPhone iOs 5, dynamically change the height of the scrollviewer based on content height not working?

What i am doing is to get data from the database. 我正在做的是从数据库中获取数据。 I then want to add that data into text views and add those textviews onto a scrollviewer. 然后,我想将这些数据添加到文本视图中,并将这些文本视图添加到scrollviewer中。

But nothing is showing up. 但是什么都没有出现。 I know that there is data but it's just not showing. 我知道有数据,但没有显示。 Most likely i'm missing to set something. 最有可能我想设置一些东西。

- (void)viewDidLoad
{
    NSUInteger height = 0;

    for (NSUInteger i = 0; i < [data count]; i++){
        DBMiscData *row = (DBMiscData *) [data objectAtIndex:i];

        if (row.text != nil){
            UITextView *tv = [[UITextView alloc] init];
            [tv setFont:[UIFont boldSystemFontOfSize:12]];
            tv.text = row.text;
            [_scrollerView addSubview:tv];
            [tv sizeToFit];
            height+=(NSUInteger)tv.frame.size.height;
        }
    }

    [_scroller setScrollEnabled:YES];
    [_scrollerView setFrame:CGRectMake(0, 0, 320, height)];
    [_scroller setContentSize:CGSizeMake(320, height)];
    [_scroller addSubview:_scrollerView];

    [super viewDidLoad];
}

Updated code and what i am after 更新的代码以及我的追求

What i want is simple same as i have in android. 我想要的东西与我在android中使用的一样简单。

android:layout_width="fill_parent" and android:layout_height="wrap_content". android:layout_width =“ fill_parent”和android:layout_height =“ wrap_content”。

CGRectFromString(aString) does not return you the rectangle in which that text fits. CGRectFromString(aString)不会返回该文本适合的矩形。 If you have a string like {{5, 10}, {100, 130}} , that method will create a CGRect for you that will have origin = CGPointMake(5,10) and size = CGSizeMake(100,130) . 如果您有{{5, 10}, {100, 130}} 100,130 {{5, 10}, {100, 130}}类的字符串,则该方法将为您创建一个CGRect,其原点= CGPointMake(5,10)和size = CGSizeMake(100,130) If you have a string like 'bananas' . 如果您有类似'bananas'的字符串。 The CGRect resulting from this will always be CGRectZero ( aka CGRectMake(0,0,0,0) ). 由此产生的CGRect将始终为CGRectZero (aka CGRectMake(0,0,0,0))。

That's why your height is zero. 这就是为什么您的身高为零。

Hope this helps. 希望这可以帮助。

Cheers! 干杯!

This might sound silly but how about using 这听起来可能很愚蠢,但是如何使用

height+=(NSUInteger)tv.frame.size.height;

instead of 代替

height+=(NSUInteger)tv.bounds.size.height;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM