Issue with vertical scrolling in UIScrollView


March 2019


1.9k time


I am using following code to create my view.

    self.percentage_ans_view = [[UIView alloc] init];
    float height = 0.0f;

    for (int i=0; i < answer_set.count; i++) {

        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20,height ,280,50 )];
        [label setText: [self.answer_set objectAtIndex:i]];
        [label setTextAlignment:NSTextAlignmentLeft];
        [label setBackgroundColor:[UIColor clearColor]];
        label.textColor = [UIColor colorWithRed:0.2941f green:0.4666f blue:0.9549f alpha:1.0f];
        label.lineBreakMode = NSLineBreakByWordWrapping;
        label.numberOfLines = 0;
        [label sizeToFit];
       // label.tag = 5000 + i;
        label.userInteractionEnabled = YES;
        label.font = [UIFont fontWithName:@"Arial" size:14.0f] ;
        [self.percentage_ans_view addSubview:label];

        height += label.frame.size.height;

        NSLog(@"LABEL SIZE height : %f  & width :  %f",label.frame.size.height,label.frame.size.height);

        NSLog(@"ans:%@", [self.answer_set objectAtIndex:i]);                   

        UIButton *ans_btn = [UIButton buttonWithType:UIButtonTypeCustom];
        ans_btn.frame = CGRectMake(0 - 5, height, (width * 3)+ 20,40 );//CGRectMake(0 - 5, 45 + (i * 80), (width * 3)+ 20,40 )
        [ans_btn setTitle:[NSString stringWithFormat:@"%d%@",percent,@"%   "] forState:UIControlStateNormal];
        ans_btn.titleLabel.font = [UIFont boldSystemFontOfSize:20];
        ans_btn.titleLabel.textColor = [UIColor whiteColor];
        ans_btn.backgroundColor = [UIColor colorWithRed:0.2941f green:0.4666f blue:0.9549f alpha:1.0f] ;

        // ans_btn.frame = frame;   your desired size
        ans_btn.clipsToBounds = YES;            

        height += ans_btn.frame.size.height;

        [self.percentage_ans_view addSubview:ans_btn];

    [self.swipe addSubview:self.percentage_ans_view];

Here self.swipe is UIScrollView which is not scrolling vertically. So i am not able to see last two labels and button of my view. What i am doing wrong ?

2 answers


Set the contentSize property of your UIScrollView properly (basically the width/height of the content that can be displayed in the scroll view without scrolling), set the pagingEnabled property of the scroll view to YES, and then add your view that you want to scroll as a subview of the UIScrollView.

As long as the lenght of the subview is more than the width of the contentSize you specified, it should scroll vertically.


If this scrollView is an IBOutlet, check if the autolayout is disabled for the parent view.

If you are using auto layout, you should add constrains to the scrollViews subviews, that will fully specify it's contentSize.