【问题标题】:Issue with vertical scrolling in UIScrollViewUIScrollView 中的垂直滚动问题
【发布时间】:2013-11-28 13:33:17
【问题描述】:

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

    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,它不会垂直滚动。所以我看不到我视图的最后两个标签和按钮。我做错了什么?

【问题讨论】:

  • 检查滚动的内容大小。

标签: ios objective-c uiview uiscrollview


【解决方案1】:

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

只要子视图的长度大于你指定的 contentSize 的宽度,它就应该垂直滚动。

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-03
    • 1970-01-01
    • 2016-07-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多