Swift 自动布局在 Xcode 9.3 中不起作用

科技嗨米

这里我有两个标签要在 Xcode 中显示。如果我不添加任何约束,它看起来不错。

无约束

无约束

在模拟器中显示

在模拟器中显示

然后我需要自动布局,以便它可以在不同的设备中正常显示。但是一旦我添加了约束,他们就会将我所有的标签移到左板上。无论我如何设置,它都不起作用。

例如,一旦我为边框设置了一个恒定的前导空间,

只有一个约束,引导空间到内容,值 15

只有一个约束,引导空间到内容,值 15,

我的标签跳到了边界。

跳到边界。同样的事情发生在 textfield

跳到边界。 同样的事情发生在 textfield

这让我困惑了近三天。我怎么能让它工作?

更新:

I tried to add possible constrains, the constraint lines shown blue, but once I ran it, the label and textfield covered each other. I don't know if there are some important things I still lost?

For label, I added a leading space constraint, and center vertically constraint. For textfield, I added a leading space to label, and vertical center constraint. When I ran it, they coverd

textfield covered label

Jon Shier

When you have no constraints, iOS uses something known at the autoresizing mask to create constraints for you at runtime, locking your views in place wherever they were in Interface Builder. These constraints won't work well for different content sizes or different devices, so you should always fully constrain your UI.

When you added the single constraint, Interface Builder told you that your interface wasn't properly constrained (the red arrow next to your view controller) and would give you guidance if you looked at that content. However, running the app with only a single constraint on that label meant that the system didn't apply the autoresizing mask like normal and the layout system essentially just set all of the other values to zero at runtime. This includes the height and width of the label, as well as its x and y position. With zeros for all of those values, what you're seeing here is the content of your label being rendered at 0,0, with a label that has 0 height and width. However, with clipsToBounds defaulted to false, the label's content is still visible, and so is shown as you see.

tl;dr:正如 Interface Builder 试图告诉您的那样,您需要限制整个 UI。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

setValuesForKeys在Xcode 9中的Swift 4中不起作用

NSKeyedArchiver在Swift 3(XCode 8)中不起作用

Xcode 8 / Swift 3:简单的UIPicker代码不起作用

一些iOS约束在Xcode 8中不起作用(转换为Swift 3后)

Xcode 9自动完成不起作用100%-部分起作用

自动布局和故事板不起作用 - Swift?

Swift自动布局:子视图中的约束不起作用

pushViewController 在 swift 3 中不起作用

CGPath在Swift 3中不起作用

Xcode自动完成功能在Swift游乐场的Sources文件夹中不起作用

如何解决 Swift 3 中的自动布局问题?

Xcode 9无线调试不起作用

与Xcode中的自动布局混淆

Swift 2.0 Defer在XCode 7.0.1中不起作用?

自定义字体在Swift / Xcode中不起作用

Xcode 中的 Swift Playground - TapGestureRecognizer 到 UILabel 不起作用

Xcode 9中的UINavigationController方法setToolbarHidden错误:自动布局约束的无限计算导致OOM

故事板 Xcode 10、iOS 中的自动布局 16:9 动画 GIF

Xcode 自动完成在 SwiftUI 中不起作用

Swift 中 FLAnimatedImageView 的自动布局

UICollectionViewCell中的自动布局不起作用

Firebase 在 Xcode 8.3 上发布 iOS 10.3 Swift 3 FCM 通知不起作用

Swift 3,Xcode 8实例化视图控制器不起作用

Swift 3 / Xcode 8.2-在viewDidLoad中切换视图不起作用

隐藏状态栏不起作用Swift 3,Xcode 8.0

WKScriptMessageHandler 不起作用 ios swift xcode

自动布局,将图片异步加载到uiimageview中(Swift,xCode6)

readWithCallback函数在Swift 3中不起作用

密码归档验证在 Swift 3 中不起作用