在UIScrollView的固定/浮动观点与自动版式 [英] Fixed / Float view in UIScrollView with AutoLayout
问题描述
在本技术说明苹果公司指出,你可以的UIScrollView的子视图固定通过添加约束UIScrollView的上海华/浮动。我想,但我做错了什么,我想不出什么问题。
请注意,您可以滚动视图的子视图出现通过创建视图和滚动视图的子树外的视图,如滚动视图的上海华之间的约束浮动(不滚动)比其他滚动内容。
块引用>这就是我所做的。我有一个UIScrollView已经建立并尝试将固定视图中添加像下面的滚动视图的顶部:
_testOverlay = [[UIView的页头]初始化];
_testOverlay.backgroundColor =的UIColor blueColor]
_testOverlay.translatesAutoresizingMaskIntoConstraints = NO;
[self.scrollView addSubview:_testOverlay];[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@| [_testOverlay] |选项:0的指标:无意见:NSDictionaryOfVariableBindings(_testOverlay)];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@V:| [_testOverlay(64)〕 - (大于= 0) - |选项:0的指标:无意见:NSDictionaryOfVariableBindings(_testOverlay)];然而,这不工作,所添加的视图将沿着与滚动视图移动并且不会浮动。任何想法什么错在这里?
解决方案
视图和滚动视图的子树之外的视图,如间
滚动视图的上海华
块引用>这部分是至关重要的。
self.scrollView
是_testOverlay
的上海华。所以,在@| [_testOverlay] |
竖线参考self.scrollView
。您有_testOverlay
键,(我想)之间的约束,以取代此约束self.view
。我不知道是否有可能与视觉格式语言,但你肯定可以使用做constraintWithItem:属性:relatedBy:toItem:属性:事半功倍:恒
。它会是这样的(我不会张贴整个code,因为它是looong):[self.view addConstraint:[NSLayoutConstraint
constraintWithItem:self.view
属性:NSLayoutAttributeLeft
relatedBy:NSLayoutRelationEqual
toItem:_testOverlay
属性:NSLayoutAttributeLeft
事半功倍:1.0
常数:0]];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?
解决方案between the view and a view outside the scroll view’s subtree, such as the scroll view’s superview.
This part is critical.
self.scrollView
is a superview of_testOverlay
. So, in@"|[_testOverlay]|"
vertical bars referenceself.scrollView
. You have to replace this constraint with the constraint between_testOverlay
and (I suppose)self.view
. I'm not sure if it's possible with the visual format language, but you certainly can do it withconstraintWithItem:attribute:relatedBy:toItem:attribute:multiplier:constant
. It would go like this (I won't post the whole code, because it's looong):[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.view attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:_testOverlay attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0]];
这篇关于在UIScrollView的固定/浮动观点与自动版式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!