Reputation: 1883
I am creating some labels, but they do not appear. My code is this. May somebody help me out?
_scrollView.pagingEnabled = YES;
NSInteger numberOfViews = 3;
for (int i = 0; i < numberOfViews; i++)
{
CGFloat xOrigin;
if (i == 0)
{
xOrigin = i * self.view.frame.size.width;
}
else
{
xOrigin = i * (self.view.frame.origin.y+self.view.frame.size.width);
}
UIView *awesomeView = [[UIView alloc] initWithFrame:CGRectMake(xOrigin, 0, self.view.frame.size.width, self.view.frame.size.height)];
awesomeView.backgroundColor = [UIColor colorWithRed:0.5/i green:0.5 blue:0.5 alpha:1];
UILabel *lbl1 = [[UILabel alloc] init];
[lbl1 setFrame:CGRectMake(30, 30, 200, 100)];
lbl1.backgroundColor=[UIColor clearColor];
lbl1.textColor=[UIColor whiteColor];
lbl1.userInteractionEnabled=NO;
lbl1.text=[NSString stringWithFormat:@"Test Plan %d",i] ;
[awesomeView addSubview:lbl1];
[_scrollView addSubview:awesomeView];
Thanks
Upvotes: 0
Views: 2446
Reputation: 966
I copied your code and tried it out. I can see the label "Test Plan 0", but there is no way to use the pagination, because you did not set the contentSize
property on your UIScrollView.
If you set scrollView.contentSize = CGSizeMake(CGRectGetWidth(scrollView.frame) * (i + 1), CGRectGetHeight(scrollView.frame));
you should the result of being able to paginate through your views.
Working example:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.window.bounds];
scrollView.pagingEnabled = YES;
NSInteger numberOfViews = 3;
for (int idx = 0; idx < numberOfViews; idx++)
{
CGRect awesomeViewFrame = self.window.frame;
awesomeViewFrame.origin.x = idx * (CGRectGetMinX(self.window.frame) + CGRectGetWidth(self.window.frame));
UIView *awesomeView = [[UIView alloc] initWithFrame:awesomeViewFrame];
awesomeView.backgroundColor = [UIColor colorWithRed:0.5/idx green:0.5 blue:0.5 alpha:1];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(30, 30, 200, 100)];
label.backgroundColor = [UIColor clearColor];
label.textColor = [UIColor whiteColor];
label.userInteractionEnabled = NO;
label.text = [NSString stringWithFormat:@"Test Plan %d", idx] ;
[awesomeView addSubview:label];
[scrollView addSubview:awesomeView];
scrollView.contentSize = CGSizeMake(CGRectGetWidth(scrollView.frame) * (idx + 1), CGRectGetHeight(scrollView.frame));
}
[self.window addSubview:scrollView];
return YES;
}
Upvotes: 1