gpt4 book ai didi

ios - XCode 自动布局不适用于 UIScrollView 进行水平分页

转载 作者:行者123 更新时间:2023-11-29 02:11:24 24 4
gpt4 key购买 nike

我正在尝试使用自动布局创建水平分页。这是代码。

 UIView *newsView = [[UIView alloc] initWithFrame:CGRectZero];
newsView.backgroundColor = [UIColor redColor];

UIView *anotherNewsView = [[UIView alloc] initWithFrame:CGRectZero];
anotherNewsView.backgroundColor = [UIColor blueColor];

self.scrollView.translatesAutoresizingMaskIntoConstraints = NO;
newsView.translatesAutoresizingMaskIntoConstraints = NO;
anotherNewsView.translatesAutoresizingMaskIntoConstraints = NO;

self.scrollView.pagingEnabled = YES;

[self.scrollView addSubview:newsView];
[self.scrollView addSubview:anotherNewsView];
[self.scrollView addConstraint:[NSLayoutConstraint constraintWithItem:newsView
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:self.scrollView
attribute:NSLayoutAttributeTop
multiplier:1.0f
constant:0.0f]];

[self.scrollView addConstraint:[NSLayoutConstraint constraintWithItem:newsView
attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationEqual
toItem:self.scrollView
attribute:NSLayoutAttributeBottom
multiplier:1.0f
constant:0.0f]];

[self.scrollView addConstraint:[NSLayoutConstraint constraintWithItem:newsView
attribute:NSLayoutAttributeLeading
relatedBy:NSLayoutRelationEqual
toItem:self.scrollView
attribute:NSLayoutAttributeLeading
multiplier:1.0f
constant:0.0f]];
//
[self.scrollView addConstraint:[NSLayoutConstraint constraintWithItem:newsView
attribute:NSLayoutAttributeTrailing
relatedBy:NSLayoutRelationEqual
toItem:anotherNewsView
attribute:NSLayoutAttributeLeading
multiplier:1.0f
constant:0.0f]];
//
[self.scrollView addConstraint:[NSLayoutConstraint constraintWithItem:anotherNewsView
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:self.scrollView
attribute:NSLayoutAttributeTop
multiplier:1.0f
constant:0.0f]];

[self.scrollView addConstraint:[NSLayoutConstraint constraintWithItem:anotherNewsView
attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationEqual
toItem:self.scrollView
attribute:NSLayoutAttributeBottom
multiplier:1.0f
constant:0.0f]];

[self.scrollView addConstraint:[NSLayoutConstraint constraintWithItem:anotherNewsView
attribute:NSLayoutAttributeTrailing
relatedBy:NSLayoutRelationEqual
toItem:self.scrollView
attribute:NSLayoutAttributeTrailing
multiplier:1.0f
constant:0.0f]];

[self.scrollView addConstraint:[NSLayoutConstraint constraintWithItem:newsView
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:anotherNewsView
attribute:NSLayoutAttributeWidth
multiplier:0.99f
constant:0.0f]];

但我只看到 scrollView,我将背景设置为青色。

enter image description here

最佳答案

问题是您的 View 的宽度和高度在当前配置时为零。

尝试向它们添加一些 with 和 height 约束,例如:

NSArray *heightConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:[view(height)]" options:0 metrics:@{ @"height":@(CGRectGetHeight(self.scrollView.frame)) } views:@{ @"view":newsView }];
[self.scrollView addConstraints: heightConstraints];

[self.scrollView addConstraints: [NSLayoutConstraint constraintsWithVisualFormat:@"H:[newsView(width)]" options:0 metrics:@{ @"width":@(CGRectGetWidth(self.scrollView.frame)) } views:@{ @"newsView":newsView]];

[self.scrollView addConstraints: [NSLayoutConstraint constraintsWithVisualFormat:@"H:[anotherNewsView(width)]" options:0 metrics:@{ @"width":@(CGRectGetWidth(self.scrollView.frame)) } views:@{ @"anotherNewsView":anotherNewsView }]];

您还可以查看我的其他答案之一 here .

重要的是 scrollview 中的 View 必须有自己的宽度和高度。这就是 scrollView 计算它的 contentSize 的方式。如果没有,contentSize 将为 CGSizeZero,您将看不到您的 View 。

希望对您有所帮助!如果您需要更多帮助,请告诉我。

关于ios - XCode 自动布局不适用于 UIScrollView 进行水平分页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29248438/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com