简体   繁体   中英

How to add UIView to UIScrollView dynamically if previous UIView is full

I have to do a pdf generator with custom label, so in my storyboard I have a UIScrollView with a UIView inside it, and if my UIView is full, I want to add another UIView and fill it with other label that is not already in. How can I add another page in my UIScrollView with another UIView ? I have to do it programmatically or there is an option in storyboard for doing it ?

I tried this (I don't know if it works and I don't want to compile it 'cause I find it really really ugly) :

CGFloat placement = titleLabel.frame.size.height + kMarginLabel;

for(int i = 0; i < [contract.nameContract count]; i++)

{

    if(placement > _contractPageView.frame.size.height)

    {

        CGFloat secondPlacement = 0;

        _scrollView.contentSize = CGSizeMake(_contractPageView.frame.size.width * 2, _contractPageView.frame.size.height);

        UIView *secondPage = [[UIView alloc] initWithFrame:_contractPageView.frame];

        articleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0 + kMarginLabel, secondPlacement, kTitleHeight, kTitleWidth)];

        articleLabel.text = [NSString stringWithFormat:@"Article %i - %@",i, [contract.nameContract objectAtIndex:i]];

        [secondPage addSubview:articleLabel];

        secondPlacement += articleLabel.frame.size.height;

        clauseLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, secondPlacement + kMarginLabel, _contractPageView.frame.size.width, kSizeOfClause)];

        clauseLabel.text = [contract.textContract objectAtIndex:i];

        secondPlacement += clauseLabel.frame.size.height;

        [secondPage addSubview:clauseLabel];

        [_scrollView addSubview:secondPage];



        if(secondPlacement > secondPage.frame.size.height)

        {

            CGFloat thirdPlacement = 0;

            _scrollView.contentSize = CGSizeMake(_contractPageView.frame.size.width * 3, _contractPageView.frame.size.height);

            UIView *thirdPage = [[UIView alloc] initWithFrame:_contractPageView.frame];

            articleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0 + kMarginLabel, thirdPlacement, kTitleHeight, kTitleWidth)];

            articleLabel.text = [NSString stringWithFormat:@"Article %i - %@",i, [contract.nameContract objectAtIndex:i]];

            [thirdPage addSubview:articleLabel];

            thirdPlacement += articleLabel.frame.size.height;

            clauseLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, thirdPlacement + kMarginLabel, _contractPageView.frame.size.width, kSizeOfClause)];

            clauseLabel.text = [contract.textContract objectAtIndex:i];

            thirdPlacement += clauseLabel.frame.size.height;

            [thirdPage addSubview:clauseLabel];

            [_scrollView addSubview:thirdPage];

        }



    } else {

        articleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, placement, kTitleHeight, kTitleWidth)];

        articleLabel.text = [NSString stringWithFormat:@"Article %i - %@",i, [contract.nameContract objectAtIndex:i]];

        [_contractPageView addSubview:articleLabel];

        placement += articleLabel.frame.size.height;

        clauseLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, placement + kMarginLabel, _contractPageView.frame.size.width, kSizeOfClause)];

        clauseLabel.text = [contract.textContract objectAtIndex:i];

        placement += clauseLabel.frame.size.height;

        [_contractPageView addSubview:clauseLabel];

    }



}

I don't know if it's the only method, and imagine if I have many and many many many object in my array, I have to create more and more page :/ I don't know how to do it :(

Thanks for your help.

I did some similar code for a generic core data editor for one of our projects. So as I see it you got two options:

  1. Use a collection view design the prototype view as your page and hook it up to an array controller that manages your text. The text could be organised as an array of NSAttributedString that you could bind to a NSTextView (one for each page)

  2. If you need more flexibility, ie pages of different sizes, different layouts etc.., you could program your own container that keep as properties an array of "Pages" (you would need to define this class) and whenever this array is changed recomputes the sizes and constraints by, for example by adding up the heights of every pages and sizing your content view appropriately.

Hope it helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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