如何在没有 Interface Builder 的情况下将 UIButton 对象的中心约束到其超级视图
How to constraint the center of a UIButton object to its superview without Interface Builder
大家好!请原谅我有点麻烦,但尽管我付出了所有努力,我还是未能让自动布局的东西在我的代码中工作。我在不使用 Xcode 的情况下构建我的 iOS 应用程序,主要是在命令行上。因此,我认为它可能会导致一些与自动布局相关的问题。请在下面找到将 UIButton 对象的中心约束到其父视图的最简单尝试。
到目前为止,我已经尝试过各种布局技术,包括锚定、NSLayotConstraint constraintWithItem: 和 VFL。 None 以上对子视图的位置有任何影响。我既没有编译错误也没有运行时错误。 TranslatesAutoresizingMaskIntoConstraints 关闭。老实说,让我的代码没有按预期运行的原因让我很吃惊。更重要的是,我浏览了许多在线可用的自动布局示例。任何帮助将不胜感激。
{
NSLayoutConstraint *constraint;
[aButton setTranslatesAutoresizingMaskIntoConstraints:NO];
constraint =
[NSLayoutConstraint
constraintWithItem:aButton
attribute:NSLayoutAttributeCenterX
relatedBy:NSLayoutRelationEqual
toItem:aButton.superview
attribute:NSLayoutAttributeCenterX
multiplier:1.0f
constant:0.0f];
constraint.active = YES;
[aButton.superview addConstraint:constraint];
constraint =
[NSLayoutConstraint
constraintWithItem:aButton
attribute:NSLayoutAttributeCenterY
relatedBy:NSLayoutRelationEqual
toItem:aButton.superview
attribute:NSLayoutAttributeCenterY
multiplier:1.0f
constant:0.0f];
constraint.active = YES;
[aButton.superview addConstraint:constraint];
}```
事实证明,故障是由我链接错误的 SDK 造成的。我应该使用 iPhoneSimulator12 而不是 iPhoneSimulator.sdk。1.sdk。
-isysroot $(ISYS_ROOT)
ISYS_ROOT = /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator12.1.sdk
万分感谢所有努力提供帮助的人。
大家好!请原谅我有点麻烦,但尽管我付出了所有努力,我还是未能让自动布局的东西在我的代码中工作。我在不使用 Xcode 的情况下构建我的 iOS 应用程序,主要是在命令行上。因此,我认为它可能会导致一些与自动布局相关的问题。请在下面找到将 UIButton 对象的中心约束到其父视图的最简单尝试。
到目前为止,我已经尝试过各种布局技术,包括锚定、NSLayotConstraint constraintWithItem: 和 VFL。 None 以上对子视图的位置有任何影响。我既没有编译错误也没有运行时错误。 TranslatesAutoresizingMaskIntoConstraints 关闭。老实说,让我的代码没有按预期运行的原因让我很吃惊。更重要的是,我浏览了许多在线可用的自动布局示例。任何帮助将不胜感激。
{
NSLayoutConstraint *constraint;
[aButton setTranslatesAutoresizingMaskIntoConstraints:NO];
constraint =
[NSLayoutConstraint
constraintWithItem:aButton
attribute:NSLayoutAttributeCenterX
relatedBy:NSLayoutRelationEqual
toItem:aButton.superview
attribute:NSLayoutAttributeCenterX
multiplier:1.0f
constant:0.0f];
constraint.active = YES;
[aButton.superview addConstraint:constraint];
constraint =
[NSLayoutConstraint
constraintWithItem:aButton
attribute:NSLayoutAttributeCenterY
relatedBy:NSLayoutRelationEqual
toItem:aButton.superview
attribute:NSLayoutAttributeCenterY
multiplier:1.0f
constant:0.0f];
constraint.active = YES;
[aButton.superview addConstraint:constraint];
}```
事实证明,故障是由我链接错误的 SDK 造成的。我应该使用 iPhoneSimulator12 而不是 iPhoneSimulator.sdk。1.sdk。
-isysroot $(ISYS_ROOT)
ISYS_ROOT = /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator12.1.sdk
万分感谢所有努力提供帮助的人。