如何使用自动布局将视图浮在滚动视图上

在iOS 6.0发行说明中有以下声明:

请注意,您可以通过在视图和滚动视图的子树之外的视图(如滚动视图的超级视图)之间创build约束来使滚动视图的子视图浮动(不滚动)其他滚动内容。

有人有主意吗?

我做了一些玩耍,我有一个工作的例子。

该视图不滚动,它被放在顶部,其高度是scrollview hight和任意值之间的差异。

// 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];