UIScrollView中的垂直滚动问题

时间:2013-11-28 13:33:17

标签: ios objective-c uiview uiscrollview

我正在使用以下代码来创建我的视图。

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

此处self.swipeUIScrollView,不会垂直滚动。所以我无法看到我的观点的最后两个标签和按钮。我做错了什么?

2 个答案:

答案 0 :(得分:2)

正确设置UIScrollView的contentSize属性(基本上可以在滚动视图中显示的内容的宽度/高度而不滚动),将滚动视图的pagingEnabled属性设置为YES,然后添加您的视图想要滚动作为UIScrollView的子视图。

只要子视图的长度超过您指定的contentSize的宽度,它就应该垂直滚动。

答案 1 :(得分:0)

如果此scrollViewIBOutlet,请检查是否为父视图禁用了自动布局。

如果您使用auto layout,则应向scrollViews子视图添加约束,这些约束将完全指定contentSize