6

In this technical Note Apple states that you can make a subview of UIScrollView fixed / floating by adding constraints to UISCrollView's superview. I tried that but I'm doing something wrong and I can't figure out whats the problem.

Note that you can make a subview of the scroll view appear to float (not scroll) over the other scrolling content by creating constraints between the view and a view outside the scroll view’s subtree, such as the scroll view’s superview.

That's what I did. I have a UIScrollView already set up and try to add the fixed view to the top of the scrollview like the following:

_testOverlay = [[UIView alloc] init];
_testOverlay.backgroundColor = [UIColor blueColor];
_testOverlay.translatesAutoresizingMaskIntoConstraints = NO;
[self.scrollView addSubview:_testOverlay];

[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|[_testOverlay]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_testOverlay)]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[_testOverlay(64)]-(>=0)-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_testOverlay)]];

However, this does not work, the added view will move along with the scrollview and does not 'float'. Any ideas whats wrong here?

4

2 回答 2

6

在视图和滚动视图的子树之外的视图之间,例如滚动视图的 superview

这部分很关键。self.scrollView是 的超级视图_testOverlay。因此,在@"|[_testOverlay]|"竖线中参考self.scrollView. _testOverlay您必须用and (I guess)之间的约束替换这个约束self.view。我不确定视觉格式语言是否可行,但您当然可以使用constraintWithItem:attribute:relatedBy:toItem:attribute:multiplier:constant. 它会是这样的(我不会发布整个代码,因为它很长):

[self.view addConstraint:[NSLayoutConstraint
                          constraintWithItem:self.view
                          attribute:NSLayoutAttributeLeft
                          relatedBy:NSLayoutRelationEqual
                          toItem:_testOverlay
                          attribute:NSLayoutAttributeLeft
                          multiplier:1.0
                          constant:0]];
于 2013-10-09T21:21:48.290 回答
0

将覆盖视图添加到滚动视图的超级视图,而不是滚动视图本身。

[self.view addSubview:_testOverlay];
于 2013-10-09T21:20:03.347 回答