ios - UIScrollView 中的垂直滚动问题

标签 ios objective-c uiview uiscrollview

我正在使用以下代码来创建我的 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];

这里的 self.swipeUIScrollView ,它不是垂直滚动的。所以我看不到我 View 的最后两个标签和按钮。我做错了什么?

最佳答案

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

只要 subview 的长度大于您指定的 contentSize 的宽度,它就应该垂直滚动。

关于ios - UIScrollView 中的垂直滚动问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20267574/

相关文章:

iphone - iOS - 使用 UIPanGestureRecognizer 平移时实时推进动画

ios - 使用 iOS Keychain Services 保存用户名-密码。如何以及安全程度如何?

ios - UIImagePickerController 中的前置摄像头

objective-c - NSURLRequest 不发送 cookie

IOS:如何动态排列 UIViews?

ios - 以编程方式更改 Xib 方向的最佳方法

Android AES 解密和来自 iOS :javax. crypto.BadPaddingException 的数据:填充 block 已损坏

iOS Metal Spritebatch - 更新顶点 VS 更新制服

ios - 使用自动调整大小时,UIView 不会在旋转时调整大小

objective-c - iOS 图像处理(失真)