如何在ios中滚动基于UITextView内容大小的scrollview

时间:2015-09-04 14:15:08

标签: ios objective-c uiscrollview uikit uitextview

我在UIViewController上添加了一个滚动视图,并且我在该滚动视图上添加了内容视图。

之后我在内容视图中添加了textview,textdata来自服务。

这里我的主要要求是滚动内容大小需要根据文本数据大小设置但是根据我的代码内容视图滚动太多但我必须根据textdata大小滚动contentview,请帮帮我。

显然,此UITextView行为即UITextView向上滚动到textdata内容大小。

我的代码: -

    #import "ViewController.h"

    @interface ViewController ()

    {
        UITextView * TextView;

        UIScrollView * scrollView;
        UIView * containerView;
    }

    @end

    @implementation ViewController

    - (void)viewDidLoad
    {
        [super viewDidLoad];

        scrollView = [[UIScrollView alloc] init];
        scrollView.backgroundColor = [UIColor redColor];
        scrollView.translatesAutoresizingMaskIntoConstraints = NO;
        [self.view addSubview:scrollView];

        containerView = [[UIView alloc] init];
        containerView.backgroundColor = [UIColor whiteColor];
        containerView.translatesAutoresizingMaskIntoConstraints = NO;
        [scrollView addSubview:containerView];

        NSDictionary * views = NSDictionaryOfVariableBindings(scrollView,containerView);

        [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[scrollView]|" options:0 metrics:nil views:views]];

        [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[scrollView(600)]|" options:0 metrics:nil views:views]];

        TextView = [[UITextView alloc]init];
        TextView.translatesAutoresizingMaskIntoConstraints = NO;
        TextView.text = 
    //Applying autolayouts for textview1
        NSLayoutConstraint * constraint2 = [NSLayoutConstraint constraintWithItem:TextView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem: containerView attribute:NSLayoutAttributeTop multiplier:1.0f constant:20.0f];
        [containerView addConstraint:constraint2];

        constraint2 = [NSLayoutConstraint constraintWithItem:TextView attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:containerView attribute:NSLayoutAttributeLeading multiplier:1.0f constant:5.0f];
        [containerView addConstraint:constraint2];

        constraint2 = [NSLayoutConstraint constraintWithItem:TextView attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:containerView attribute:NSLayoutAttributeTrailing multiplier:1.0f constant:-5.0f];
        [containerView addConstraint:constraint2];

        constraint2 = [NSLayoutConstraint constraintWithItem:TextView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0f constant:1000];
        [containerView addConstraint:constraint2];


 [scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[containerView(==scrollView)]|"
                                                                           options:0
                                                                           metrics:nil
                                                                             views:views]];

        [scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[containerView(1200)]|"
                                                                           options:0
                                                                           metrics:nil
                                                                             views:views]];
    }

0 个答案:

没有答案