Patrick
Patrick

Reputation: 5592

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.

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:layout_width="fill_parent" and android:layout_height="wrap_content".

Upvotes: 0

Views: 1522

Answers (2)

Alperen Aydin
Alperen Aydin

Reputation: 57

This might sound silly but how about using

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

instead of

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

Upvotes: 0

George
George

Reputation: 4029

CGRectFromString(aString) does not return you the rectangle in which that text fits. 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). If you have a string like 'bananas' . The CGRect resulting from this will always be CGRectZero ( aka CGRectMake(0,0,0,0) ).

That's why your height is zero.

Hope this helps.

Cheers!

Upvotes: 1

Related Questions