在iOS 6.0 Release notes有如下语句:
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.
有人有什么想法吗?
最佳答案
我做了一些尝试,我有一个可以工作的例子。
View 不滚动,它被放在顶部,高度是 ScrollView 高度和任意值之间的差值。
// in viewDidLoad
UIIView *myView = [[UIView alloc] init];
[self.scrollView addSubview:myView];
myView.translatesAutoresizingMaskIntoConstraints = NO;
NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem:self.mapView
attribute:NSLayoutAttributeTop
relatedBy:(NSLayoutRelationEqual)
toItem:self.containerView
attribute:(NSLayoutAttributeTop)
multiplier:1.0
constant:0];
[self.view addConstraint:constraint];
// Give my view some intrinsic size
NSDictionary *dict = NSDictionaryOfVariableBindings(myView);
NSArray *constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"|[myView]|"
options:(NSLayoutFormatAlignAllBaseline)
metrics:nil
views:dict];
[self.view addConstraints:constraints];
// In view did appear
// calc height from height of scroll view - this is needs work
float height = -self.scrollView.frame.size.height + 250;
NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem:self.mapView attribute:NSLayoutAttributeHeight
relatedBy:(NSLayoutRelationEqual)
toItem:self.containerView
attribute:(NSLayoutAttributeHeight)
multiplier:1.0
constant:offset];
[self.view addConstraint:constraint];
关于ios - 如何使用自动布局使 View float 在 ScrollView 上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14927532/