gpt4 book ai didi

iOS 使用自动布局以编程方式修复 UIScrollView 中 subview 的位置

转载 作者:行者123 更新时间:2023-11-28 20:01:13 31 4
gpt4 key购买 nike

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

Apple 表示现在可以使用 Autolayout 来完成此操作,但它似乎对我不起作用。下面的代码正确显示了 UIScrollView 及其 subview ,但 map 仍然与其他所有内容一起滚动。我是否漏掉了一些非常明显的东西?

https://developer.apple.com/LIBRARY/ios/technotes/tn2154/_index.html

请注意,您可以通过在 View 和 ScrollView 子树外部的 View (例如 ScrollView 的父 View )之间创建约束,使 ScrollView 的 subview 看起来 float (而不是滚动)在其他滚动内容上.

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

编辑:

jrturton 的回答很准确。这是最终有效的代码:

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

最佳答案

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

您需要使用更长的方法 (constraintWithItem...) 来创建单独的约束,其中 item1 是您的 float View ,item2 是 ScrollView 的父 View 。

关于iOS 使用自动布局以编程方式修复 UIScrollView 中 subview 的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23919236/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com