Xcode 8 UIButtons 约束未显示

Xcode 8 UIButtons with constraints not showing up(Xcode 8 UIButtons 约束未显示)

本文介绍了Xcode 8 UIButtons 约束未显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

过去一切都很好,直到昨天我将 Xcode 更新到版本 8.

Everything used to work great until yesterday I updated Xcode to version 8.

如果我不使用任何约束,我可以看到按钮,但是当我对按钮应用约束时,它们会消失,无论约束是什么或多少(高度、垂直间距、尾随等...)我申请了.

If I don't use any constraints, I can see the buttons but when I apply constraint(s) to the buttons they disappear, no matter what or how many constraints (height, vertical spacing, trailing etc...) I apply.

当我点击按钮时,它们表现正常(事件总是被发送,所以我假设它们实际上位于那里(?)).我尝试更改文本颜色、背景颜色(甚至添加新的 UIButton),但没有成功.我看不到按钮.当我更改 Centered View 的背景颜色时,我可以在应用程序上看到此更改.最奇怪的部分是当我在 Xcode 上单击 Debug View Hierarchy 以查看发生了什么时,那里的一切看起来都很好;我可以看到所有按钮都按预期定位.

When I tap buttons, they act normal (events are always sent so I assume they are actually located there(?)). I tried changing text color, background color, (even adding a new UIButton) but ended with no luck. I can't see the buttons. When I change the background color of Centered View I can see this change on the app. The weirdest part is when I click Debug View Hierarchy on Xcode to see what is going on, everything looks fine there; I can see all buttons located as I expect.

在 iPhone 5 (iOS 9.3.5) 和 iPhone 6 (iOS 10) 上测试应用程序.

Testing the app both on iPhone 5 (iOS 9.3.5) and iPhone 6 (iOS 10).

我正在使用 Swift 2.3、iOS 10 和 Xcode 8.

I am using Swift 2.3, iOS 10 and Xcode 8.

这是 Xcode 8 上的错误吗?如何解决这个问题?

Is this a bug on Xcode 8? How to resolve this issue?

更新:

我刚刚意识到 UILabel 和 UIImageView 也会出现同样的约束问题.它们在没有约束的情况下看起来很好,但是当我添加任何约束时它们就会消失.

I just realised same constraint problem occurs on UILabel and UIImageView too. They look fine without constraint but when I add any constraint they disappear.

推荐答案

我错过了将 UIButton 类设置为我的自定义类的要点.我检查了我的自定义类,发现我在 awakeFromNib() 方法上设置了圆角半径.我在设置圆角半径之前调用了 layoutIfNeeded(),这是我的解决方案.

I was missing the point of setting UIButton class to my custom class. I checked my custom class and have seen that I set corner radius on awakeFromNib() method. I called layoutIfNeeded() before I set corner radius which was the solution in my case.

这篇关于Xcode 8 UIButtons 约束未显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:Xcode 8 UIButtons 约束未显示

基础教程推荐