Создайте динамическое подпредставление uiscrollview

Мне было интересно, можно ли динамически создать подвид (страницу) для моего UIScrollView. Мне просто нужно, чтобы было 2 UILabel. Один сверху и один снизу. Если это возможно, может ли кто-нибудь сказать мне, как это сделать, или указать мне учебник/видео?

Я использовал учебник, и у меня все получилось очень хорошо. Но у него только 1 UILabel внизу. Ссылка на него находится здесь http://www.iosdevnotes.com/2011/03/uiscrollview-paging/

Это то, где я думаю, что большая часть действия.

_objects = [[NSArray alloc] initWithObjects:@"",@"Does not include today", @"This does include today", @"Does not include today", @"This does include today", @"Includes the weekends!", nil];

_detailLabel1 = [[UILabel alloc] initWithFrame:CGRectMake(100.0, 0.0, 320.0, 460.0)];
_detailLabel1.textAlignment = UITextAlignmentCenter;
_detailLabel1.font = [UIFont boldSystemFontOfSize:15.0];
_detailLabel1.textColor=[UIColor whiteColor];
_detailLabel1.backgroundColor=[UIColor colorWithWhite:(CGFloat)1 alpha:(CGFloat)0];


_detailLabel2 = [[UILabel alloc] initWithFrame:CGRectMake(320.0, 0.0, 320.0, 460.0)];
_detailLabel2.textAlignment = UITextAlignmentCenter;
_detailLabel2.font = [UIFont boldSystemFontOfSize:15.0];
_detailLabel2.textColor=[UIColor whiteColor];
_detailLabel2.backgroundColor=[UIColor colorWithWhite:(CGFloat)1 alpha:(CGFloat)0];


_detailLabel3 = [[UILabel alloc] initWithFrame:CGRectMake(640.0, 0.0, 320.0, 460.0)];
_detailLabel3.textAlignment = UITextAlignmentCenter;
_detailLabel3.font = [UIFont boldSystemFontOfSize:15.0];
_detailLabel3.textColor=[UIColor whiteColor];
_detailLabel3.backgroundColor=[UIColor colorWithWhite:(CGFloat)1 alpha:(CGFloat)0];
// We are going to show all the contents of the _objects array
// using only these three UILabel instances, making them jump 
// right and left, replacing them as required:

[scrollView addSubview:_detailLabel1];
[scrollView addSubview:_detailLabel2];
[scrollView addSubview:_detailLabel3];

У меня есть другой код в других местах, но здесь, я думаю, основная его часть.


person Alex    schedule 30.10.2011    source источник


Ответы (1)


Вместо добавления меток непосредственно в scrollview. Создайте подвид, затем добавьте метки в подвид, а затем добавьте в прокрутку. Итак, ваша работа сделана.

кусок кода, взятый из руководств, на которые вы ссылались.

NSArray *colors = [NSArray arrayWithObjects:[UIColor redColor], [UIColor greenColor], [UIColor blueColor], nil];
    for (int i = 0; i < colors.count; i++) {
        CGRect frame;
        frame.origin.x = self.scrollView.frame.size.width * i;
        frame.origin.y = 0;
        frame.size = self.scrollView.frame.size;

        UIView *subview = [[UIView alloc] initWithFrame:frame];
        subview.backgroundColor = [colors objectAtIndex:i];
        [self.scrollView addSubview:subview];

_detailLabel1 = [[UILabel alloc] initWithFrame:CGRectMake(100.0, 0.0, 100.0, 40.0)];
_detailLabel1.textAlignment = UITextAlignmentCenter;
_detailLabel1.font = [UIFont boldSystemFontOfSize:15.0];
_detailLabel1.textColor=[UIColor whiteColor];
_detailLabel1.backgroundColor=[UIColor colorWithWhite:(CGFloat)1 alpha:(CGFloat)0];
 [subview addSubview:_detailLabel1];

_detailLabel2 = [[UILabel alloc] initWithFrame:CGRectMake(100.0, 300.0, 100.0, 40.0)];
_detailLabel2.textAlignment = UITextAlignmentCenter;
_detailLabel2.font = [UIFont boldSystemFontOfSize:15.0];
_detailLabel2.textColor=[UIColor whiteColor];
_detailLabel2.backgroundColor=[UIColor colorWithWhite:(CGFloat)1 alpha:(CGFloat)0];
[subview addSubview:_detailLabel2];


        [subview release];
    }
person Anand    schedule 31.10.2011