布局,在我的项目中,我使用自动布局在视图控制器上添加了scrollview,并在此滚动视图中添加了一个内容视图
所以一切都很好,但我想根据内容大小滚动内容视图,为此我搜索了很多,但我没有得到结果请帮帮我
根据我的代码屏幕显示如下所示我剩下的数据不滚动我在这里做错了什么(这里滚动必须匹配所有i-phone和i-pad英寸)
#import "ViewController3.h"
@interface ViewController3 ()
{
UIScrollView * scrollView;
UIView * containerView;
UITextView * TextView;
}
@end
@implementation ViewController3
- (void)viewDidLoad {
[super viewDidLoad];
scrollView = [[UIScrollView alloc] init];
scrollView.backgroundColor = [UIColor lightGrayColor];
scrollView.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:scrollView];
containerView = [[UIView alloc] init];
containerView.backgroundColor = [UIColor redColor];
containerView.translatesAutoresizingMaskIntoConstraints = NO;
[scrollView addSubview:containerView];
//Applying autolayouts for scrollview
NSLayoutConstraint * constraint1 = [NSLayoutConstraint constraintWithItem:scrollView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem: self.view attribute:NSLayoutAttributeTop multiplier:1.0f constant:0.0f];
[self.view addConstraint:constraint1];
constraint1 = [NSLayoutConstraint constraintWithItem:scrollView attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeading multiplier:1.0f constant:0.0f];
[self.view addConstraint:constraint1];
constraint1 = [NSLayoutConstraint constraintWithItem:scrollView attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTrailing multiplier:1.0f constant:0.0f];
[self.view addConstraint:constraint1];
constraint1 = [NSLayoutConstraint constraintWithItem:scrollView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeBottom multiplier:1.0f constant:0.0f];
[self.view addConstraint:constraint1];
TextView = [UITextView new];
TextView.translatesAutoresizingMaskIntoConstraints = NO;
TextView.text = @"In December 2012, Tendulkar announced his retirement from ODIs.[23] He retired from Twenty20 cricket in October 2013[24] and subsequently announced his retirement from all forms of cricket,[25][26] retiring on 16 November 2013 after playing his 200th and final Test match, against the West Indies in Mumbai's Wankhede Stadium.[27] Tendulkar played 664 international cricket matches in total, scoring 34,357 runs.[8]In December 2012, Tendulkar announced his retirement from ODIs.[23] He retired from Twenty20 cricket in October 2013[24] and subsequently announced his retirement from all forms of cricket,[25][26] retiring on 16 November 2013 after playing his 200th and final Test match, against the West Indies in Mumbai's Wankhede Stadium.[27] Tendulkar played 664 international cricket matches in total, scoring 34,357 runs.[8]In December 2012, Tendulkar announced his retirement from ODIs.[23] He retired from Twenty20 cricket in October 2013[24] and subsequently announced his retirement from all forms of cricket,[25][26] retiring on 16 November 2013 after playing his 200th and final Test match, against the West Indies in Mumbai's Wankhede Stadium.[27] Tendulkar played 664 international cricket matches in total, scoring 34,357 runs.[8]In December 2012, Tendulkar announced his retirement from ODIs.[23] He retired from Twenty20 cricket in October 2013[24] and subsequently announced his retirement from all forms of cricket,[25][26] retiring on 16 November 2013 after playing his 200th and final Test match, against the West Indies in Mumbai's Wankhede Stadium.[27] Tendulkar played 664 international cricket matches in total, scoring 34,357 runs.[8]In December 2012, Tendulkar announced his retirement from ODIs.[23] He retired from Twenty20 cricket in October 2013[24] and subsequently announced his retirement from all forms of cricket,[25][26] retiring on 16 November 2013 after playing his 200th and final Test match, against the West Indies in Mumbai's Wankhede Stadium.[27] Tendulkar played 664 international cricket matches in total, scoring 34,357 runs.[8]In December 2012, Tendulkar announced his retirement from ODIs.[23] He retired from Twenty20 cricket in October 2013[24] and subsequently announced his retirement from all forms of cricket,[25][26] retiring on 16 November 2013 after playing his 200th and final Test match, against the West Indies in Mumbai's Wankhede Stadium.[27] Tendulkar played 664 international cricket matches in total, scoring 34,357 runs.[8]In December 2012, Tendulkar announced his retirement from ODIs.[23] He retired from Twenty20 cricket in October 2013[24] and subsequently announced his retirement from all forms of cricket,[25][26] retiring on 16 November 2013 after playing his 200th and final Test match, against the West Indies in Mumbai's Wankhede Stadium.[27] Tendulkar played 664 international cricket matches in total, scoring 34,357 runs.[8]";
TextView.backgroundColor = [UIColor lightGrayColor];
[containerView addSubview:TextView];
//Applying autolayouts for textview1
NSLayoutConstraint * constraint = [NSLayoutConstraint constraintWithItem:TextView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem: containerView attribute:NSLayoutAttributeTop multiplier:1.0f constant:20.0f];
[containerView addConstraint:constraint];
constraint = [NSLayoutConstraint constraintWithItem:TextView attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:containerView attribute:NSLayoutAttributeLeading multiplier:1.0f constant:5.0f];
[containerView addConstraint:constraint];
constraint = [NSLayoutConstraint constraintWithItem:TextView attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:containerView attribute:NSLayoutAttributeTrailing multiplier:1.0f constant:-5.0f];
[containerView addConstraint:constraint];
constraint = [NSLayoutConstraint constraintWithItem:TextView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:containerView attribute:NSLayoutAttributeHeight multiplier:1 constant:600];
[containerView addConstraint:constraint];
//Applying autolayouts for containerview
NSLayoutConstraint * constraint2 = [NSLayoutConstraint constraintWithItem:containerView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem: scrollView attribute:NSLayoutAttributeTop multiplier:1.0f constant:0.0f];
[scrollView addConstraint:constraint2];
constraint2 = [NSLayoutConstraint constraintWithItem:containerView attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:scrollView attribute:NSLayoutAttributeLeading multiplier:1.0f constant:0.0f];
[scrollView addConstraint:constraint2];
constraint2 = [NSLayoutConstraint constraintWithItem:containerView attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:scrollView attribute:NSLayoutAttributeTrailing multiplier:1.0f constant:0.0f];
[scrollView addConstraint:constraint2];
constraint2 = [NSLayoutConstraint constraintWithItem:containerView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:scrollView attribute:NSLayoutAttributeBottom multiplier:1.0f constant:-10.0f];
[scrollView addConstraint:constraint2];
constraint2 = [NSLayoutConstraint constraintWithItem:containerView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:scrollView attribute:NSLayoutAttributeWidth multiplier:1 constant:0];
[scrollView addConstraint:constraint2];
constraint2 = [NSLayoutConstraint constraintWithItem:containerView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:scrollView attribute:NSLayoutAttributeHeight multiplier:1 constant:0];
[scrollView addConstraint:constraint2];
}
答案 0 :(得分:0)
检查出来:
UIScrollView * scroll = ...
[self.view addSubview: scroll];
UIView * scrollWrapper = ...
[scroll addSubview: scrollWrapper];
// each new view will be added as a subview of "scrollWrapper"
UIView *mainView = self.view;
NSDictionary *viewsDictionary = NSDictionaryOfVariableBindings(scroll, scrollWrapper, mainView);
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[scroll]|" options:0 metrics:nil views:viewsDictionary]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[scroll]|" options:0 metrics:nil views:viewsDictionary]];
[scroll addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[scrollWrapper]|" options:0 metrics:nil views:viewsDictionary]];
[scroll addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[scrollWrapper]|" options:0 metrics:nil views:viewsDictionary]];
/* add constraints for subviews */
[scrollWrapper addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"<VFL Constraint>" options:0 metrics:nil views:viewsDictionary]];
/* this is the key to make the content scrollable */
[mainView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[scrollWrapper(==mainView)]" options:0 metrics:0 views:viewsDictionary]];