如何根据ios中的字段大小滚动滚动滚动视图

如何根据ios中的字段大小滚动滚动滚动视图,ios,swift,scroll,uiscrollview,autolayout,Ios,Swift,Scroll,Uiscrollview,Autolayout,我是iOS自动布局的初学者。在我的项目中,我以编程方式在视图控制器上添加了一个UIScrollView,并且还向所述滚动视图添加了一个内容视图 之后,我在内容视图中添加了两个自动增长的文本视图。根据数据大小,文本视图的大小会自行增长,即文本视图的数据来自服务 我想向上滚动内容视图,以便用户可以在任何iPhone/iPad屏幕上看到两个自动增长的文本视图,如4、5、6、6 Plus、iPad 我已经写了一些代码,但是滚动视图并没有向上滚动以使所有字段对用户可见。谁能帮帮我吗 根据我的代码,屏幕如下

我是iOS自动布局的初学者。在我的项目中,我以编程方式在视图控制器上添加了一个UIScrollView,并且还向所述滚动视图添加了一个内容视图

之后,我在内容视图中添加了两个自动增长的文本视图。根据数据大小,文本视图的大小会自行增长,即文本视图的数据来自服务

我想向上滚动内容视图,以便用户可以在任何iPhone/iPad屏幕上看到两个自动增长的文本视图,如4、5、6、6 Plus、iPad

我已经写了一些代码,但是滚动视图并没有向上滚动以使所有字段对用户可见。谁能帮帮我吗

根据我的代码,屏幕如下所示,用户看不到剩余数据:

自动增长文本视图:- 视图控制器:-
如果要根据字符串的内容大小启用滚动,请参考此解决方案。

对于我的问题,这不是完美的解决方案
#import "AutoGrowingTextView.h"

@implementation AutoGrowingTextView

- (void) layoutSubviews
{
    [super layoutSubviews];

    if (!CGSizeEqualToSize(self.bounds.size, [self intrinsicContentSize])) {
        [self invalidateIntrinsicContentSize];
    }
}

- (CGSize)intrinsicContentSize
{
    CGSize intrinsicContentSize = self.contentSize;

    // iOS 7.0+
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0f) {
        intrinsicContentSize.width += (self.textContainerInset.left + self.textContainerInset.right ) / 2.0f;
        intrinsicContentSize.height += (self.textContainerInset.top + self.textContainerInset.bottom) / 2.0f;
    }

    return intrinsicContentSize;
}

@end
  #import "ViewController.h"

    #import "AutoGrowingTextView.h"

    @interface ViewController ()

    {
        AutoGrowingTextView * TextView;

        AutoGrowingTextView * TextView1;

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

        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 = [AutoGrowingTextView new];

        TextView.translatesAutoresizingMaskIntoConstraints = NO;

        TextView.text = @"De Villiers currently holds the record for the fastest 50, fastest 100 and fastest 150 in ODIs. The team that has suffered the most at the hands of this right-handed genius has been the West Indies.De Villiers currently holds the record for the fastest 50, fastest 100 and fastest 150 in ODIs. The team that has suffered the most at the hands of this right-handed genius has been the West Indies.De Villiers currently holds the record for the fastest 50, fastest 100 and fastest 150 in ODIs. The team that has suffered the most at the hands of this right-handed genius has been the West Indies.De Villiers currently holds the record for the fastest 50, fastest 100 and fastest 150 in ODIs. The team that has suffered the most at the hands of this right-handed genius has been the West Indies.De Villiers currently holds the record for the fastest 50, fastest 100 and fastest 150 in ODIs. The team that has suffered the most at the hands of this right-handed genius has been the West Indies.De Villiers currently holds the record for the fastest 50, fastest 100 and fastest 150 in ODIs. The team that has suffered the most at the hands of this right-handed genius has been the West Indies.De Villiers currently holds the record for the fastest 50, fastest 100 and fastest 150 in ODIs. The team that has suffered the most at the hands of this right-handed genius has been the West Indies.De Villiers currently holds the record for the fastest 50, fastest 100 and fastest 150 in ODIs. The team that has suffered the most at the hands of this right-handed genius has been the West Indies.De Villiers currently holds the record for the fastest 50, fastest 100 and fastest 150 in ODIs. The team that has suffered the most at the hands of this right-handed genius has been the West Indies.";

        TextView.backgroundColor = [UIColor colorWithRed:0.95 green:0.47 blue:0.48 alpha:1.0];

        [containerView addSubview:TextView];

        TextView1 = [AutoGrowingTextView new];

        TextView1.translatesAutoresizingMaskIntoConstraints = NO;

        TextView1.text = @"He smashed the fastest ODI off just 16 balls during the second ODI against West Indies in Johannesburg in January 2015. He had broken the 19-year mark off 17 balls set by Sanath Jayasuriya against Pakistan in Singapore in 1996.He smashed the fastest ODI off just 16 balls during the second ODI against West Indies in Johannesburg in January 2015. He had broken the 19-year mark off 17 balls set by Sanath Jayasuriya against Pakistan in Singapore in 1996.He smashed the fastest ODI off just 16 balls during the second ODI against West Indies in Johannesburg in January 2015. He had broken the 19-year mark off 17 balls set by Sanath Jayasuriya against Pakistan in Singapore in 1996.He smashed the fastest ODI off just 16 balls during the second ODI against West Indies in Johannesburg in January 2015. He had broken the 19-year mark off 17 balls set by Sanath Jayasuriya against Pakistan in Singapore in 1996.He smashed the fastest ODI off just 16 balls during the second ODI against West Indies in Johannesburg in January 2015. He had broken the 19-year mark off 17 balls set by Sanath Jayasuriya against Pakistan in Singapore in 1996.He smashed the fastest ODI off just 16 balls during the second ODI against West Indies in Johannesburg in January 2015. He had broken the 19-year mark off 17 balls set by Sanath Jayasuriya against Pakistan in Singapore in 1996.He smashed the fastest ODI off just 16 balls during the second ODI against West Indies in Johannesburg in January 2015. He had broken the 19-year mark off 17 balls set by Sanath Jayasuriya against Pakistan in Singapore in 1996.He smashed the fastest ODI off just 16 balls during the second ODI against West Indies in Johannesburg in January 2015. He had broken the 19-year mark off 17 balls set by Sanath Jayasuriya against Pakistan in Singapore in 1996.";

        TextView1.backgroundColor = [UIColor colorWithRed:0.95 green:0.47 blue:0.48 alpha:1.0];

        [containerView addSubview:TextView1];

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

        //Applying autolayouts for textview2

        constraint2 = [NSLayoutConstraint constraintWithItem:TextView1 attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem: TextView attribute:NSLayoutAttributeBottom multiplier:1.0f constant:20.0f];

        [containerView addConstraint:constraint2];

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

        [containerView addConstraint:constraint2];

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

        [containerView addConstraint:constraint2];

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

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

    @end