0

我正在尝试使用苹果开发链接上提到的“混合方法”的示例代码:

我试图在 UIScrollView 中垂直堆叠 3 个视图。在下面的示例中,UIScrollView 在加载时显示红色视图,但未按预期滚动。我可以滚动一点以查看红色视图下方的绿色视图 - 但滚动视图会弹回并且不会滚动到绿色视图或它下方的视图(蓝色视图)。我知道我需要一个约束,我试图在视图 1 和 2 之间添加一个约束,以便 view2.top = view1.bottom ,但似乎我遗漏了一些东西。

我还注意到滚动视图的内容大小为零(在 viewDidAppear 方法中)。

关于我所缺少的任何提示或有关如何使这种混合方法发挥作用的帮助都会很棒!

    - (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.


    UIScrollView* scrollView = ((UIScrollView*)self.view);

    scrollView.translatesAutoresizingMaskIntoConstraints = NO;

    CGFloat w = self.view.frame.size.width;
    CGFloat h = self.view.frame.size.height;

    contentView = [[UIView alloc] initWithFrame:CGRectMake(0,0, w, h*3)];

    [scrollView addSubview:contentView];


    v1 =[[UIView alloc] initWithFrame:CGRectMake(0,0, w, h)];
    v2 =[[UIView alloc] initWithFrame:CGRectMake(0,h, w, h)];
    v3 =[[UIView alloc] initWithFrame:CGRectMake(0,h*2, w, h)];

    v1.backgroundColor = [UIColor redColor];
    v2.backgroundColor = [UIColor greenColor];
    v3.backgroundColor = [UIColor blueColor];


    [contentView addSubview:v1];
    [contentView addSubview:v2];
    [contentView addSubview:v3];

    NSLayoutConstraint *myConstraint =[NSLayoutConstraint
                                       constraintWithItem:v1
                                       attribute:NSLayoutAttributeBottom
                                       relatedBy:NSLayoutRelationEqual
                                       toItem:v2
                                       attribute:NSLayoutAttributeTop
                                       multiplier:1.0
                                       constant:0];

    [contentView addConstraint:myConstraint];

    scrollView.contentSize = contentView.bounds.size;

}
4

2 回答 2

2

如果您只是想在滚动视图中堆叠视图,我不明白为什么需要约束。我尝试了您的代码,但它并没有像您说的那样滚动良好,但我认为这是因为您使用 UIScrollView 作为视图控制器的主视图,您是否有特定的原因要这样做?

我更改了代码,改为将 UIScrollView 添加到普通 UIView 中,它可以按预期完美运行,而无需使用任何复杂的约束。只需记住使用在顶部定义滚动视图UIScrollView *scrollView;

- (void)viewDidLoad {
    [super viewDidLoad];

    scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
    [self.view addSubview:scrollView];

    CGFloat w = self.view.frame.size.width;
    CGFloat h = self.view.frame.size.height;

    contentView = [[UIView alloc] initWithFrame:CGRectMake(0,0, w, h*3)];
    [scrollView addSubview:contentView];

    v1 =[[UIView alloc] initWithFrame:CGRectMake(0,0, w, h)];
    v2 =[[UIView alloc] initWithFrame:CGRectMake(0,h, w, h)];
    v3 =[[UIView alloc] initWithFrame:CGRectMake(0,h*2, w, h)];

    v1.backgroundColor = [UIColor redColor];
    v2.backgroundColor = [UIColor greenColor];
    v3.backgroundColor = [UIColor blueColor];

    [contentView addSubview:v1];
    [contentView addSubview:v2];
    [contentView addSubview:v3];

    scrollView.contentSize = contentView.bounds.size;
}
于 2013-11-15T05:03:43.633 回答
0

我不得不为 UIScrollView 与愚蠢的纯自动布局方法作斗争。我想要一个基于子视图的滚动视图,这些子视图基于自动布局约束并与旋转一起使用。最后,以下“混合方法”代码为我工作了垂直滚动并保持简单:

 - (void)viewDidLoad {
    UIView *contentView;

    contentView = [[UIView alloc] initWithFrame:self.scrollView.bounds];

    //don't add UIViewAutoresizingFlexibleHeight as that messes things up but next line helps with rotation
    contentView.autoresizingMask = UIViewAutoresizingFlexibleWidth; 

    [scrollView addSubview:contentView];

    // DON'T change contentView's translatesAutoresizingMaskIntoConstraints,
    // which defaults to YES;

    // Set the content size of the scroll view to match the size of the content view
    [scrollView setContentSize:CGSizeMake(contentWidth, contentHeight)];

     /* the rest of your code here... pre auto-layout style and you can change the origins of subviews to position them within contentView */
      [contentView addSubview:subView1];
      [contentView addSubview:subView2];
    }

要实现的关键点是,不要在初始化后的任何时候更改 contentView 的大小并将其添加到滚动视图,因为这会导致其内部约束中断,但您仍然可以继续添加子视图而不必担心它的大小。

除非您使用我还没有测试过的 systemLayoutSizeFittingSize: 方法,否则您将独立于 contentView 的大小来计算滚动视图的内容大小

于 2015-08-05T15:40:29.707 回答