iOS使用Autolayout以编程方式固定UIScrollView中子视图的位置

iOS使用Autolayout以编程方式固定UIScrollView中子视图的位置,ios,objective-c,uiscrollview,mkmapview,autolayout,Ios,Objective C,Uiscrollview,Mkmapview,Autolayout,我有一个包含UIScrollView的UIView。UIScrollView包含一个MKMapView子视图及其正下方的占位符子视图。我想将MKMapView固定在屏幕顶部,并允许占位符子视图在其上滑动并覆盖它 苹果表示,现在可以通过自动布局来实现这一点,但它似乎不适合我。下面的代码正确地显示了UIScrollView及其子视图,但地图仍然与其他所有内容一起滚动。我是不是错过了一些很明显的东西 请注意,通过在视图和滚动视图子树外的视图(如滚动视图的superview)之间创建约束,可以使滚动视

我有一个包含UIScrollView的UIView。UIScrollView包含一个MKMapView子视图及其正下方的占位符子视图。我想将MKMapView固定在屏幕顶部,并允许占位符子视图在其上滑动并覆盖它

苹果表示,现在可以通过自动布局来实现这一点,但它似乎不适合我。下面的代码正确地显示了UIScrollView及其子视图,但地图仍然与其他所有内容一起滚动。我是不是错过了一些很明显的东西

请注意,通过在视图和滚动视图子树外的视图(如滚动视图的superview)之间创建约束,可以使滚动视图的子视图在其他滚动内容上浮动(而不是滚动)

UIView.m文件:

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {

        // Create scroll view and add to view
        UIScrollView *scrollView = [[UIScrollView alloc] init];
        [scrollView setBackgroundColor: [UIColor clearColor]];
        [scrollView setShowsVerticalScrollIndicator:NO];
        [scrollView setTranslatesAutoresizingMaskIntoConstraints:NO];
        [self addSubview:scrollView];

        // Create map view and add to scroll view
        mapView = [[MKMapView alloc] init];
        mapView.showsPointsOfInterest = NO;
        mapView.translatesAutoresizingMaskIntoConstraints = NO;
        [scrollView addSubview:mapView];

        // Create a placeholder image to scroll over map view
        UIImageView *randomPlaceholderStuff = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"stuff.png"]];
        [dividingLine setTranslatesAutoresizingMaskIntoConstraints:NO];
        [scrollView addSubview:dividingLine];


        // Layouts
        NSDictionary *viewArranging = NSDictionaryOfVariableBindings(scrollView, tourMap, randomPlaceholderStuff);

        [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[scrollView]|"
                                                                     options:0
                                                                     metrics:0
                                                                       views:viewArranging]];

        [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[scrollView]|"
                                                                     options:0
                                                                     metrics:0
                                                                       views:viewArranging]];

        UIView *referenceSuperView = scrollView.superview;

        [referenceSuperView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[mapView(320)]|"
                                                                           options:0
                                                                           metrics:0
                                                                             views:viewArranging]];

        [referenceSuperView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[mapView(320)]"
                                                                           options:0
                                                                           metrics:0
                                                                             views:viewArranging]];

        [scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-334-[randomPlaceholderStuff]|"
                                                                           options:0
                                                                           metrics:0
                                                                             views:viewArranging]];

        [scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[randomPlaceholderStuff(320)]|"
                                                                           options:0
                                                                           metrics:0
                                                                             views:viewArranging]];

    }
    return self;
}

@end
编辑:

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {

        // Create scroll view and add to view
        UIScrollView *scrollView = [[UIScrollView alloc] init];
        [scrollView setBackgroundColor: [UIColor clearColor]];
        [scrollView setShowsVerticalScrollIndicator:NO];
        [scrollView setTranslatesAutoresizingMaskIntoConstraints:NO];
        [self addSubview:scrollView];

        // Create map view and add to scroll view
        mapView = [[MKMapView alloc] init];
        mapView.showsPointsOfInterest = NO;
        mapView.translatesAutoresizingMaskIntoConstraints = NO;
        [scrollView addSubview:mapView];

        // Create a placeholder image to scroll over map view
        UIImageView *randomPlaceholderStuff = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"stuff.png"]];
        [dividingLine setTranslatesAutoresizingMaskIntoConstraints:NO];
        [scrollView addSubview:dividingLine];


        // Layouts
        NSDictionary *viewArranging = NSDictionaryOfVariableBindings(scrollView, tourMap, randomPlaceholderStuff);

        [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[scrollView]|"
                                                                     options:0
                                                                     metrics:0
                                                                       views:viewArranging]];

        [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[scrollView]|"
                                                                     options:0
                                                                     metrics:0
                                                                       views:viewArranging]];

        UIView *referenceSuperView = scrollView.superview;

        [referenceSuperView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[mapView(320)]|"
                                                                           options:0
                                                                           metrics:0
                                                                             views:viewArranging]];

        [referenceSuperView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[mapView(320)]"
                                                                           options:0
                                                                           metrics:0
                                                                             views:viewArranging]];

        [scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-334-[randomPlaceholderStuff]|"
                                                                           options:0
                                                                           metrics:0
                                                                             views:viewArranging]];

        [scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[randomPlaceholderStuff(320)]|"
                                                                           options:0
                                                                           metrics:0
                                                                             views:viewArranging]];

    }
    return self;
}

@end
杰特顿的回答恰到好处。以下是最终有效的代码:

[self addConstraint:[NSLayoutConstraint constraintWithItem:mapView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:scrollView.superview attribute:NSLayoutAttributeTop multiplier:1.0 constant:0.0]];

[self addConstraint:[NSLayoutConstraint constraintWithItem:mapView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:scrollView.superview attribute:NSLayoutAttributeWidth multiplier:1.0 constant:0.0]];

[self addConstraint:[NSLayoutConstraint constraintWithItem:mapView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:320.0]];

你不能用视觉格式语言做你想做的事。
|
字符将始终被解释为视图的直接superview,因此您不会创建您认为是的约束


您需要使用较长的方法(
constraintWithItem…
)来创建单个约束,其中item1是浮动视图,item2是滚动视图的超级视图

你不能用视觉格式语言做你正在尝试的事情。
|
字符将始终被解释为视图的直接superview,因此您不会创建您认为是的约束


您需要使用较长的方法(
constraintWithItem…
)来创建单个约束,其中item1是浮动视图,item2是滚动视图的超级视图

你不能用视觉格式语言做你正在尝试的事情。
|
字符将始终被解释为视图的直接superview,因此您不会创建您认为是的约束


您需要使用较长的方法(
constraintWithItem…
)来创建单个约束,其中item1是浮动视图,item2是滚动视图的超级视图

你不能用视觉格式语言做你正在尝试的事情。
|
字符将始终被解释为视图的直接superview,因此您不会创建您认为是的约束


您需要使用较长的方法(
constraintWithItem…
)来创建单个约束,其中item1是浮动视图,item2是滚动视图的超级视图

通过将子视图添加/移动到scrollview的超级视图,可以在
UIScrollView
上添加浮动子视图,如:

如本快照所示,将按钮放置/设置在滚动视图上(不在滚动视图内)。还可以设置相对于scrollview超级视图的按钮约束(位置)

这里是每个视图相对位置的层次结构的参考快照


通过将子视图添加/移动到scrollview的超级视图,可以在
UIScrollView
上添加浮动子视图,如:

如本快照所示,将按钮放置/设置在滚动视图上(不在滚动视图内)。还可以设置相对于scrollview超级视图的按钮约束(位置)

这里是每个视图相对位置的层次结构的参考快照


通过将子视图添加/移动到scrollview的超级视图,可以在
UIScrollView
上添加浮动子视图,如:

如本快照所示,将按钮放置/设置在滚动视图上(不在滚动视图内)。还可以设置相对于scrollview超级视图的按钮约束(位置)

这里是每个视图相对位置的层次结构的参考快照


通过将子视图添加/移动到scrollview的超级视图,可以在
UIScrollView
上添加浮动子视图,如:

如本快照所示,将按钮放置/设置在滚动视图上(不在滚动视图内)。还可以设置相对于scrollview超级视图的按钮约束(位置)

这里是每个视图相对位置的层次结构的参考快照