drbj
drbj

Reputation: 115

AddSubView is not working in UIScrollView

I need to place my labels and other objects into a scrollable view. But it doesn't add my 2 labels, I only get a white background everytime I run my program

- (void) initLayout:(CGRect)frame
{
self.backgroundColor = [UIColor whiteColor];

UIFont *customFont  = [UIFont fontWithName:@"HelveticaNeue" size:40.0]; //custom font

scrollView                  = [[UIScrollView alloc] initWithFrame:frame];
[self addSubview:scrollView];

oponent                 = [[UILabel alloc] init];
oponent.font            = customFont;
oponent.textColor       = [UIColor blackColor];
oponent.textAlignment   = NSTextAlignmentCenter;
oponent.text            = @"TEST";
[scrollView addSubview:oponent];

proponent                 = [[UILabel alloc] init];
proponent.font            = customFont;
proponent.textColor       = [UIColor blackColor];
proponent.textAlignment   = NSTextAlignmentCenter;
proponent.text            = @"TRY";
[scrollView addSubview:proponent];


}

Upvotes: 0

Views: 814

Answers (2)

Khushboo Motwani
Khushboo Motwani

Reputation: 205

You Haven't set frame to label

oponent                 = [[UILabel alloc] init];

proponent                 = [[UILabel alloc] init];

set frames

CGRect labelframe           // Customize according to your need

oponent  = [[UILabel alloc] initWithFrame:labelframe];  // missing 

proponent = [[UILabel alloc] initWithFrame:labelframe];  // missing 

Upvotes: 1

Fawad Masud
Fawad Masud

Reputation: 12344

You are not setting frames for proponent and opponent labels. Set their frames and also set content size for scrollview. You can also check by setting some border colors to your labels.

To check if your scrollview is working add UIScrollViewDelegate in your header file and then use the delegate methods.

scrollView.delegate = self;
scrollView.scrollEnabled = YES;

Add the following delegate method and use breakpoint to check if it is called

- (void)scrollViewDidScroll:(UIScrollView *)scrollView

{

}

Upvotes: 2

Related Questions