以编程方式添加约束不起作用
Posted
技术标签:
【中文标题】以编程方式添加约束不起作用【英文标题】:Adding Constraint Programmatically Does Not Work 【发布时间】:2020-05-01 21:33:28 【问题描述】:我正在像这样以编程方式设置图像视图的约束:
imgScrollView = UIScrollView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
imgScrollView.showsHorizontalScrollIndicator = false
imgScrollView.showsVerticalScrollIndicator = false
imgScrollView.bouncesZoom = false
imgScrollView.bounces = false
view.addSubview(imgScrollView)
imgScrollView.translatesAutoresizingMaskIntoConstraints = false
imgScrollView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 0).isActive = true
imgScrollView.bottomAnchor.constraint(equalTo: toolBar.topAnchor, constant: -100).isActive = true
imgScrollView.leftAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leftAnchor, constant: 0).isActive = true
imgScrollView.rightAnchor.constraint(equalTo: view.safeAreaLayoutGuide.rightAnchor, constant: 0).isActive = true
// add image view to scrollview
imgView = UIImageView(frame: CGRect(x: 0, y: 0,width: 100, height: 100))
imgScrollView.addSubview(imgView)
imgView.translatesAutoresizingMaskIntoConstraints = false
imgView.topAnchor.constraint(equalTo: imgScrollView.contentLayoutGuide.topAnchor, constant: 0).isActive = true
imgView.bottomAnchor.constraint(equalTo: imgScrollView.contentLayoutGuide.bottomAnchor, constant: 0).isActive = true
imgView.leftAnchor.constraint(equalTo: imgScrollView.contentLayoutGuide.leftAnchor, constant: 0).isActive = true
imgView.rightAnchor.constraint(equalTo: imgScrollView.contentLayoutGuide.rightAnchor, constant: 0).isActive = true
imgView.widthAnchor.constraint(equalTo: imgScrollView.widthAnchor, multiplier: 1).isActive = true
现在稍后点击按钮,我将添加额外的约束
imgView.heightAnchor.constraint(equalTo: imgScrollView.heightAnchor, multiplier: 1).isActive = true
但是没有添加约束。为什么会这样?
【问题讨论】:
@mezzi 为什么不直接在开头添加高度约束,然后在点击按钮时更改其常量? 你想做什么?给予左右约束并给予宽度......它们会发生冲突......底部和顶部的情况也是如此......也给予高度...... 当我在创建 imgView 的开头添加所有约束左、右和宽度、高度时没有问题。没有冲突。一切都按预期工作。当我尝试在按钮点击后添加高度约束时,没有添加约束 @badhanganesh 你是什么意思?我应该在开头输入 nil 吗? 我不知道你在点击按钮时做了什么,但不是在按钮点击上添加新的高度约束,你可以在开头添加高度约束,高度为零或其他东西(或任何默认值),然后点击按钮,您可以将该高度常数更改为您想要的任何新值。您必须存储要更改的(高度)约束。 【参考方案1】:您的实现逻辑对我有用。
左图显示没有高度限制的实现。 右图显示按钮按下高度限制。在函数viewDidLoad中
override func viewDidLoad()
super.viewDidLoad()
view.backgroundColor = .white
scrollView.isScrollEnabled = true
scrollView.bounces = false
scrollView.alwaysBounceVertical = true
imageView.image = UIImage(color: .lightGray)
navigationItem.rightBarButtonItem = barButtonItem
view.addSubview(scrollView)
scrollView.addSubview(imageView)
scrollView.translatesAutoresizingMaskIntoConstraints = false
imageView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
scrollView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
scrollView.leftAnchor.constraint(equalTo: view.leftAnchor),
scrollView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor),
scrollView.rightAnchor.constraint(equalTo: view.rightAnchor)
])
NSLayoutConstraint.activate([
imageView.topAnchor.constraint(equalTo: scrollView.contentLayoutGuide.topAnchor),
imageView.leftAnchor.constraint(equalTo: scrollView.contentLayoutGuide.leftAnchor),
imageView.bottomAnchor.constraint(equalTo: scrollView.contentLayoutGuide.bottomAnchor),
imageView.rightAnchor.constraint(equalTo: scrollView.contentLayoutGuide.rightAnchor),
imageView.widthAnchor.constraint(equalTo: scrollView.widthAnchor)
])
在功能按钮按下
@objc func buttonPressed()
navigationItem.rightBarButtonItem = nil
UIView.animate(withDuration: 1)
NSLayoutConstraint.activate([
self.imageView.heightAnchor.constraint(equalTo: self.scrollView.heightAnchor)
])
self.view.setNeedsLayout()
self.view.layoutIfNeeded()
UIImage 工具Create UIImage with solid color in Swift
fileprivate extension UIImage
convenience init?(color: UIColor, size: CGSize = CGSize(width: 1, height: 1))
let rect = CGRect(origin: .zero, size: size)
UIGraphicsBeginImageContextWithOptions(rect.size, false, 0.0)
color.setFill()
UIRectFill(rect)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
guard let cgImage = image?.cgImage else return nil
self.init(cgImage: cgImage)
【讨论】:
使用 NSLayoutConstraint.activate() 设置约束就可以了。不是它按预期工作。谢谢。以上是关于以编程方式添加约束不起作用的主要内容,如果未能解决你的问题,请参考以下文章