ios - 如何使用自动布局使 View float 在 ScrollView 上

标签 ios ios6

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/

相关文章:

iphone - tableHeaderView 的自动布局

ios - 在 iOS 6 中没有调用 shouldAutorotateToInterfaceOrientation

ios - 键盘显示部分 ios6

android - 将 React-Native bundle 图像传递给原生模块

ios - 是否可以在纯 Objective-C 中编写 iOS 8 应用程序?

iOS - 开始 iOS 教程 - 变量前的下划线?

ios - 标注按钮使应用程序崩溃 - MapKit

ios - 使用 PDF 矢量图像作为内容时 CALayer 模糊

ios - MobileVLCKit 播放器在 iOS 11 上显示黑屏

ios - 如何让 UI TableView 在屏幕外启动?