如何使用autolayout使视图浮动在滚动视图上 [英] How to make a view float over a scroll view using autolayout

查看:120
本文介绍了如何使用autolayout使视图浮动在滚动视图上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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


请注意,您可以使滚动视图的子视图显示浮动
(通过在视图和滚动视图的子树外部的视图之间创建约束
来覆盖其他滚动内容,例如
滚动视图的超级视图。

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.

任何人都有任何想法吗?

Anyone have any ideas?

推荐答案

我做了一些游戏,我有一个各种工作的例子。

I did some playing around and I have a working example of sorts.

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

The view does not scroll, it gets put at the top, with a height that is the difference between the scrollview hight and an arbitrary value.

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

这篇关于如何使用autolayout使视图浮动在滚动视图上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆