我想在图像视图顶部添加一个小的叠加视图,其中包含文本“原始”,只有在用户查看原始图像时才能看到。
这是我的toggleImage函数,当用户按下它时,图像View将显示原始图像(以及带有文本的视图" Original"将弹出)然后返回到过滤后的图像用户举起手来。
func toggleImage(sender: UILongPressGestureRecognizer) {
sender.minimumPressDuration = 0.3
if sender.state == .Began{
imageView.image = self.rawImage!
view.addSubview(originalText)
}else if sender.state == .Ended{
imageView.image = self.newImage
}
现在我想用文本为视图设置约束。
func toggleImage(sender: UILongPressGestureRecognizer) {
sender.minimumPressDuration = 0.3
if sender.state == .Began{
imageView.image = self.rawImage!
view.addSubview(originalText)
originalText.translatesAutoresizingMaskIntoConstraints = false
let bottomConstraint = originalText.bottomAnchor.constraintEqualToAnchor(view.topAnchor)
let leftConstriant = originalText.leftAnchor.constraintEqualToAnchor(view.leftAnchor)
let rightConstraint = originalText.rightAnchor.constraintEqualToAnchor(view.rightAnchor)
let heightConstraint = originalText.heightAnchor.constraintEqualToConstant(44)
NSLayoutConstraint.activateConstraints([bottomConstraint,leftConstriant,rightConstraint,heightConstraint])
view.layoutIfNeeded()
}else if sender.state == .Ended{
imageView.image = self.newImage
}
现在无法显示subView,如何解决问题?
这是未设置约束时模拟器的屏幕截图。 screenshot
答案 0 :(得分:0)
你的底部等于顶部:
let bottomConstraint = originalText.bottomAnchor.constraintEqualToAnchor(view.topAnchor)
您的视图最有可能在屏幕外/视图范围之外呈现。
将其更改为:
let topConstraint = originalText.topAnchor.constraintEqualToAnchor(view.topAnchor)