简体   繁体   English

UIView的宽度和高度未适应约束

[英]UIView width & height not adjusting to constraints

This is a question about iOS, XCode, Auto Layout, and Objective-C. 这是有关iOS,XCode,自动版式和Objective-C的问题。 I feel like it has an easy answer, but I've searched high and low in SO and can't find a solution. 我觉得它有一个简单的答案,但我在SO中搜索过高而又低,因此找不到解决方案。

I want to place a small crosshairs image in the center of a Google Map so that as the user pans around the map, the image remains fixed. 我想在Google地图的中心放置一个较小的十字线图像,以便在用户平移地图时,图像保持固定。 I'm doing this by simply adding a UIImageView as a subview to the Google Map view, which is a GMSMapView that I've made an IBOutlet, self.mapView. 为此,我只需将UIImageView作为子视图添加到Google Map视图中,该视图就是我创建的IBOutlet的GMSMapView self.mapView。

I calculate the location to place image with the following: 我使用以下方法计算放置图像的位置:

crosshairs.center = CGPointMake(self.mapView.bounds.size.width / 2, self.mapView.bounds.size.height / 2);

Meanwhile, I've set constraints on the map's view so that its leading and trailing spaces are pegged to the superview. 同时,我在地图视图上设置了约束,以使其前导和尾随空间与超级视图挂钩。 That should make it's width and height liquid -- or so I thought. 那应该使它的宽度和高度成为液体-或我想。

In fact, when I NSLog the width and height of the map view at runtime, they reflect the width (600) and height (384) as specified in the size inspector in Xcode -- even though I have NOT pinned the width nor height with constraints. 实际上,当我在运行时使用NSLog记录地图视图的宽度和高度时,它们会反映Xcode的大小检查器中指定的宽度(600)和高度(384)-即使我没有用限制。

The result is that the image is centered not to the screen, but to the map view's erroneous width and height (which extend off screen). 结果是图像不是居中放置在屏幕上,而是居中放置在地图视图的错误的宽度和高度(它们延伸到屏幕之外)的中心。

I know I'm overlooking something stupid, or I'm missing essential concepts of Auto Layout. 我知道我忽略了一些愚蠢的事情,或者我错过了自动版式的基本概念。 Either way, I'd appreciate any guidance. 无论哪种方式,我都会感谢您的指导。

Will

The important thing while using auto layout, is you should not be changing the frame manually anymore (using setCenter: in your case). 使用自动布局时,重要的是您不再需要手动更改框架(在您的情况下使用setCenter: You should layout everything using constraints. 您应该使用约束来布局所有内容。

You could add the UIImage in the storyboard on top of the mapView and center it there, using constraints. 您可以使用约束将UIImage添加到mapView顶部的情节提要中,并在其中居中。

If that is not possible, you can also add the constraints in code, using something like this: 如果这不可能,那么您还可以使用以下类似的方法在代码中添加约束:

[self.mapView addSubview:crosshairs];
[crosshairs setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.mapView addConstraints:[NSLayoutConstraint constraintWithItem:self.mapView attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:crosshairs attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0]];
[self.mapView addConstraints:[NSLayoutConstraint constraintWithItem:self.mapView attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:crosshairs attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0]];

Depending on where you are adding these constraints, you might have to call [self.mapView layoutIfNeeded]; 根据添加这些约束的位置,您可能必须调用[self.mapView layoutIfNeeded]; .

Using the constraints, the image should always be centered on top of the mapView. 使用约束,图像应始终位于mapView顶部的中心。

About the wrong frame for the mapView, it depends where you are logging it. 关于mapView的框架错误,这取决于您在哪里记录。 If your IBOutlet is in a UIViewController , you should do it in viewWillLayoutSubviews: after the [super viewWillLayoutSubviews]; 如果您的IBOutletUIViewController ,则应该在viewWillLayoutSubviews: [super viewWillLayoutSubviews]; call. 呼叫。

- (void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];
    NSLog(@"Map View frame %@", NSStringFromCGRect(self.mapView.frame));
}

If you have it in a custom UIView class, you should check the frame in layoutSubviews: 如果您在自定义UIView类中拥有它,则应检查layoutSubviews:的框架layoutSubviews:

- (void)layoutSubviews {
    [super layoutSubviews];
    NSLog(@"Map View frame %@", NSStringFromCGRect(self.mapView.frame));
}

Hope this fixes your problem. 希望这可以解决您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM