iOS-纯自动布局和UIScrollView不滚动
这是我第一次使用纯自动布局方法的UIScrollView。这就是视图层次结构的外观iOS-纯自动布局和UIScrollView不滚动,ios,ios7,uiscrollview,autolayout,Ios,Ios7,Uiscrollview,Autolayout,这是我第一次使用纯自动布局方法的UIScrollView。这就是视图层次结构的外观 view -scrollview --view1 --view2 --view3 scrollview应按该顺序包含view1 | view2 | view3 我将ScrollView的宽度、高度、centerx和底部空间设置为superview。创建的view1、view2和view3都在其updateConstraints方法中设置了宽度和高度约束。此外,代码中还提供了一些约束。这个滚动视图没有从左向右滚动
view
-scrollview
--view1
--view2
--view3
scrollview应按该顺序包含view1 | view2 | view3
我将ScrollView的宽度、高度、centerx和底部空间设置为superview。创建的view1、view2和view3都在其updateConstraints方法中设置了宽度和高度约束。此外,代码中还提供了一些约束。这个滚动视图没有从左向右滚动的原因是什么?我已经阅读了所有我可以在网上找到的关于使用auto layout以编程方式创建子视图并将其添加到UIScrollView的指南。我发现有人提到必须为作为scrollview的子视图添加的每个视图提供四种不同的约束:前导、尾随、顶部和底部。这些是唯一可以指定的NSLayoutAttribute吗?NSLayoutAttributeLeft或NSLayoutAttributeRight等属性如何关联?我也读过苹果网站上的文档,特别是。我正在附加当前的设置。一切都是通过代码完成的
- (void)viewDidLoad
{
[super viewDidLoad];
self.dataSource = @[ [[PCCGenericRating alloc] initWithTitle:@"Easiness"
andMessage:@"WHAT A JOKERRRR"
andVariatons:@[ @"very easy", @"easy", @"moderate", @"hard", @"very hard"]],
[[PCCGenericRating alloc] initWithTitle:@"Joker"
andMessage:@"WHAT A JOKERRRR"
andVariatons:@[ @"very easy", @"easy", @"moderate", @"hard", @"very hard"]],
[[PCCGenericRating alloc] initWithTitle:@"Difficulty"
andMessage:@"YOu are not difficult at all"
andVariatons:@[ @"very easy", @"easy", @"moderate", @"hard", @"very hard"]]
];
[self initView];
}
- (void)initView {
CGFloat navigationBarHeight = self.navigationController.navigationBar.frame.size.height;
CGFloat statusBarHeight = [[UIApplication sharedApplication] statusBarFrame].size.height;
CGFloat heightDifference = navigationBarHeight + statusBarHeight;
self.scrollView = [[UIScrollView alloc] init];
self.scrollView.delegate = self;
[self.scrollView setTranslatesAutoresizingMaskIntoConstraints:NO];
self.scrollView.backgroundColor = [UIColor greenColor];
[self.view addSubview:self.scrollView];
//setup constraints
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.scrollView attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeWidth
multiplier:1.0f
constant:0.0f]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.scrollView attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeHeight
multiplier:1.0f
constant:-heightDifference]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.scrollView attribute:NSLayoutAttributeCenterX
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeCenterX
multiplier:1.0f
constant:0.0f]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.scrollView attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeBottom
multiplier:1.0f
constant:0.0]];
[self.dataSource enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
PCCGenericRating *rating = (PCCGenericRating *)obj;
PCCGenericRatingView *ratingView = [self createViewWithRating:rating];
[self.scrollView addSubview:ratingView];
int multiplier = (idx == 0) ? 1 : (int) (idx + 1) ;
[self.scrollView addConstraint:[NSLayoutConstraint constraintWithItem:ratingView
attribute:NSLayoutAttributeCenterX
relatedBy:NSLayoutRelationEqual
toItem:self.scrollView
attribute:NSLayoutAttributeCenterX
multiplier:multiplier
constant:0.0f]];
[self.scrollView addConstraint:[NSLayoutConstraint constraintWithItem:ratingView
attribute:NSLayoutAttributeCenterY
relatedBy:NSLayoutRelationEqual
toItem:self.scrollView
attribute:NSLayoutAttributeCenterY
multiplier:1.0f
constant:0.0f]];
}];
}
- (PCCGenericRatingView *)createViewWithRating:(PCCGenericRating *)rating {
PCCGenericRatingView *view = [PCCGenericRatingView genericRatingViewWithTitle:rating.title andMessage:rating.message];
return view;
}
打印出scrollview约束后,我觉得它们看起来很好:
po self.scrollView.constraints
<__NSArrayM 0x115b051f0>(
<NSLayoutConstraint:0x1145d9290 PCCGenericRatingView:0x114579880.centerX == UIScrollView:0x11458d4b0.centerX>,
<NSLayoutConstraint:0x1145d9410 PCCGenericRatingView:0x114579880.centerY == UIScrollView:0x11458d4b0.centerY>,
<NSLayoutConstraint:0x1145d9dd0 PCCGenericRatingView:0x1145d9560.centerX == 2*UIScrollView:0x11458d4b0.centerX>,
<NSLayoutConstraint:0x1145d9e40 PCCGenericRatingView:0x1145d9560.centerY == UIScrollView:0x11458d4b0.centerY>,
<NSLayoutConstraint:0x1145da6b0 PCCGenericRatingView:0x1145d9e90.centerX == 3*UIScrollView:0x11458d4b0.centerX>,
<NSLayoutConstraint:0x1145da730 PCCGenericRatingView:0x1145d9e90.centerY == UIScrollView:0x11458d4b0.centerY>
)
po self.scrollView.constraints
(
,
,
,
,
,
)
下面是它的屏幕截图:
我觉得奇怪的是,数据源中的最后一个元素是scrollview中显示的第一个视图控制器,而它应该是最后一个视图。它也不会像应该的那样从左向右滚动。无论您在哪里打印约束,请尝试打印scrollview.contentSize,它很可能是0,0,这就是您的问题所在。据我所知,正如您在文章中提到的,您必须显式地将scrollview的子视图设置为scrollview上下左右约束。设置这些选项将自动设置scrollview的contentSize,从而使其能够滚动。看起来您只是在设置centerX和centerY约束,而这些约束不会将scrollviews contentSize设置为所需的大小 尝试以编程方式设置这些(这是伪代码,但您知道了):
- view1.topConstraint=scrollView.topConstraint
- view1.leftConstraint=scrollView.leftConstraint
- view3.bottomConstraint=滚动视图.bottomConstraint
- view3.rightConstraint=scrollView.rightConstraint
如果所有这些设置都正确,您的scrollview将正确滚动。请记住检查contentsize,如果contentsize为0,0,则约束设置不正确 确保视图1的
顶部约束
和视图3的底部约束
符合滚动视图的约束。否则,ScLabVIEW的“代码>内容大小:{ 0, 0 } <代码> ./p>尝试从VIEW中调用iNITVIEW,因为您的布局的所有维度都没有设置在VIELDDIAD中。这不会改变任何东西。我相信有一些约束是不加的。我只是不知道什么。我认为这是最重要的事情要考虑。谢谢:)