【问题标题】:How to scroll the scrollview according to fields sizes in ios如何根据ios中的字段大小滚动滚动视图
【发布时间】:2015-11-30 11:54:37
【问题描述】:

我是 iOS 自动布局的初学者。在我的项目中,我以编程方式在 View Controller 上添加了一个 UIScrollView,并且我还在上述 Scroll View 中添加了一个 Content View。

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

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

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

根据我的代码,屏幕如下所示,其余数据对用户不可见:

AutoGrowingTextView:-

#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

【问题讨论】:

    标签: ios swift scroll uiscrollview autolayout


    【解决方案1】:

    如果您想根据字符串的内容大小启用滚动,请参考此解决方案。 How to make this auto layout arrangement work with scrollview

    【讨论】:

    • 这不是我的问题的完美解决方案
    猜你喜欢
    • 1970-01-01
    • 2018-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-25
    • 2023-04-11
    • 2012-06-22
    • 1970-01-01
    相关资源
    最近更新 更多