Nic*_*ico 5 uilabel ios autolayout swift
我有一个UIView,我是通过Interface Builder在ViewController中添加的。
在我的代码中,每次按下按钮时,我都会在此UIView中创建一个标签:
func addToDisplay(stringToDisplay: String) {
let label = UILabel(frame: CGRectMake(0, 0, 10, 10))
label.text = stringToDisplay
label.textColor = UIColor.redColor()
label.textAlignment = .Right
label.sizeToFit()
label.adjustsFontSizeToFitWidth = true
label.minimumScaleFactor = 0.5
label.setTranslatesAutoresizingMaskIntoConstraints(false)
var lastLabel: UILabel?
if displayView.subviews.count > 0 {
lastLabel = displayView.subviews[displayView.subviews.count - 1] as? UILabel
}
displayView.addSubview(label)
var viewsDictionary = ["displayView": displayView, "label": label]
// add constraints
if let lastLabel = lastLabel {
viewsDictionary["lastLabel"] = lastLabel
displayView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:[label(==20)]-0-[lastLabel]", options: nil, metrics: nil, views: viewsDictionary))
} else {
displayView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:[label(==20)]-0-|", options: nil, metrics: nil, views: viewsDictionary))
}
displayView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[label]-0-|", options: nil, metrics: nil, views: viewsDictionary))
}
Run Code Online (Sandbox Code Playgroud)
当我按下按钮时,设备上(模拟器上也没有)上什么也没有出现,但是当我查看视图调试层次结构时,它就在那里。我不明白为什么。任何的想法?
编辑:
这是两个屏幕截图:


编辑2:
通过将alpha更改为不同于0的值来解决...
好吧,我认为你需要告诉视图更新约束,写下这个并检查:
[displayView updateConstraintsIfNeeded];
Run Code Online (Sandbox Code Playgroud)
还要测试您是否displayView已添加到self.view!您使用导航栏吗?如果是的话是吗translucent?如果是,那么您的 self.view 可能从(0,0)开始,导航栏位于其顶部。如果是这种情况,则给出一些x 和 y,例如(100, 100)并检查它是否显示。
让我们知道这是否有帮助。
| 归档时间: |
|
| 查看次数: |
2405 次 |
| 最近记录: |