如何根据域滚动滚动视图大小在IOS [英] How to scroll the scrollview according to fields sizes in ios
问题描述
我和自动布局于iOS初学者。要滚动查看说在我的项目,我以编程方式添加的视图控制器一个UIScrollView,我还增加了一个内容的浏览。
I am a beginner with Auto-Layouts for iOS. In my project, I programmatically add a UIScrollView on the View Controller, and I also added a Content View to said Scroll View.
在这之后,我添加了两个自动增长的文本视图来查看内容。根据数据大小,文本视图大小不断增长他们的自我(即对文本视图中的数据是从业务推出)。
After that, I added two auto-growing Text Views to the Content View. According to data size, Text Views sizes are growing them selves (i.e. the data for the Text Views is coming from services).
我要滚动的内容查看了这么两个自动增长的文本视图是任何iPhone / iPad屏幕上对用户可见(如4,5,6,6此外,新iPad)。
I want to scroll that Content View up so that two auto-growing Text Views are visible to the user on any iPhone/iPad screen (like 4, 5, 6, 6 Plus, iPad).
我已经写了一些code,但滚动视图不向上滚动,使各个领域对用户可见。可能有人请帮助我吗?
I have written some code but the Scroll View is not scrolling up to make all fields visible to the user. Could someone please help me?
和根据我的code出现在屏幕如下所示,其余的数据是不可见的用户:
And according to my code the screen appears like shown below and the remaining data is not visible to the user:
#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
的ViewController: -
#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
推荐答案
如果您想启用滚动为每串比是指该解决方案的内容大小。
<一href=\"http://stackoverflow.com/questions/32395102/how-to-make-this-auto-layout-arrangement-work-with-scrollview/32395943\">How使滚动视图与这自动版面安排工作
if you want to enable scrolling as per your Content Size of String than refer this solution. How to make this auto layout arrangement work with scrollview
这篇关于如何根据域滚动滚动视图大小在IOS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!