创建动态uiscrollview子视图

问题描述 投票:0回答:1

我想知道是否可以为我的

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];

我在其他地方还有其他代码,但我认为这是它的主要部分。

iphone dynamic pagination uiscrollview subview
1个回答
0
投票

而不是直接向滚动视图添加标签。创建一个子视图,然后将标签添加到子视图,然后添加到滚动视图。这样你的工作就完成了。

从您引用的教程中获取的代码。

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];
    }
© www.soinside.com 2019 - 2024. All rights reserved.