如何在Xcode中添加此约束?看来我的relatedBy:论证是错误的 [英] How do I add this constraint in Xcode? It seems my relatedBy: argument is wrong

查看:62
本文介绍了如何在Xcode中添加此约束?看来我的relatedBy:论证是错误的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码如下,以及我的应用图片。基本上我想在 viewDidLoad 方法中为此视图控制器添加一个约束,以对齐两个标签,使它们从相同的x位置开始。无论出于何种原因,我都会收到此警告:

My code is below, as well as the image of my app. Basically I want to add a constraint in the viewDidLoad method for this view controller to align the two labels so they start at the same x position. For whatever reason, I'm getting this warning, though:


指向整数转换的不兼容指针将'UIView *'发送到类型'的参数NSLayoutRelation'(又名'enum NSLayoutRelation');

Incompatible pointer to integer conversion sending 'UIView *' to parameter of type 'NSLayoutRelation' (aka 'enum NSLayoutRelation');

当我运行应用程序时,我收到以下错误:

And when I run the app I get the following error:


*由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:'* + [NSLayoutConstraint constraintWithItem:attribute:relatedBy:toItem:attribute:multiplier:constant:]:未知布局属性'

* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* +[NSLayoutConstraint constraintWithItem:attribute:relatedBy:toItem:attribute:multiplier:constant:]: Unknown layout attribute'

NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem:self.carMakeLabel attribute:NSLayoutRelationEqual relatedBy:self.view toItem:self.carModelLabel attribute:NSLayoutAttributeLeading multiplier:1.0 constant:0];

[self.view addConstraint:constraint];


推荐答案

尝试:

NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem:self.carMakeLabel attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.carModelLabel attribute:NSLayoutAttributeLeading multiplier:1.0 constant:0];

[self.view addConstraint:constraint];

确保self.carModelLabel和self.carMakeLabel都在self.view的视图层次结构中。

Make sure self.carModelLabel and self.carMakeLabel are both in the view hierarchy of self.view.

这篇关于如何在Xcode中添加此约束?看来我的relatedBy:论证是错误的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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