Ios 以编程方式添加约束不起作用
我以编程方式设置图像视图的约束,如下所示:Ios 以编程方式添加约束不起作用,ios,swift,nslayoutconstraint,Ios,Swift,Nslayoutconstraint,我以编程方式设置图像视图的约束,如下所示: imgScrollView=UIScrollView(帧:CGRect(x:0,y:0,宽度:100,高度:100)) imgScrollView.showshorizontalscrolindicator=false imgScrollView.showsVerticalScrollIndicator=false imgScrollView.bouncesZoom=false imgScrollView.bounces=false view.addS
imgScrollView=UIScrollView(帧:CGRect(x:0,y:0,宽度:100,高度:100))
imgScrollView.showshorizontalscrolindicator=false
imgScrollView.showsVerticalScrollIndicator=false
imgScrollView.bouncesZoom=false
imgScrollView.bounces=false
view.addSubview(imgScrollView)
imgScrollView.translatesAutoResizengmaskintoConstraints=false
imgScrollView.topAnchor.constraint(equalTo:view.safeAreaLayoutGuide.topAnchor,常量:0)。isActive=true
imgScrollView.bottomAnchor.constraint(等式:toolBar.topAnchor,常数:-100)。isActive=true
imgScrollView.leftAnchor.constraint(equalTo:view.safeAreaLayoutGuide.leftAnchor,常量:0)。isActive=true
imgScrollView.rightAnchor.constraint(等式:view.safeAreaLayoutGuide.rightAnchor,常量:0)。isActive=true
//将图像视图添加到scrollview
imgView=UIImageView(帧:CGRect(x:0,y:0,宽度:100,高度:100))
imgScrollView.addSubview(imgView)
imgView.translatesAutoresizingMaskIntoConstraints=false
imgView.topAnchor.constraint(等式:imgScrollView.contentLayoutGuide.topAnchor,常量:0)。isActive=true
imgView.bottomAnchor.constraint(等式:imgScrollView.contentLayoutGuide.bottomAnchor,常量:0)。isActive=true
imgView.leftAnchor.constraint(等式:imgScrollView.contentLayoutGuide.leftAnchor,常量:0)。isActive=true
imgView.rightAnchor.constraint(等式:imgScrollView.contentLayoutGuide.rightAnchor,常量:0)。isActive=true
imgView.widthAnchor.constraint(等式:imgScrollView.widthAnchor,乘数:1)。isActive=true
现在,稍后通过按钮点击,我将添加额外的约束
imgView.heightAnchor.constraint(等式:imgScrollView.heightAnchor,乘数:1)。isActive=true
但是,未添加约束。为什么会发生这种情况?您的实现逻辑适合我
- 左图显示无高度约束的实现李>
- 右图像显示按钮在高度限制下按下李>
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-util
@mezzi为什么不在开始时添加高度约束,并在点击按钮时更改其常量?好吧,你想做什么?给左右约束,也给宽度与他们。。。他们会发生冲突。。。底部和顶部的情况也是如此……同时给出高度……当我在开始时添加所有的约束左、右和宽度、高度时,当创建imgView时,没有问题。没有冲突。一切正常。当我尝试在点击按钮后添加高度约束时,约束没有被激活added@badhanganesh什么意思?我是否应该在开始时将“零”作为常量?我不知道在“按钮点击”上执行什么操作,但不必在“按钮点击”上添加新的高度约束,您可以在“高度零”或其他值(或任何默认值)的开头添加高度约束,然后在“按钮点击”上,您可以将该高度常量更改为您想要的任何新值。您必须存储要更改的(高度)约束。使用NSLayoutConstraint.activate()设置约束就可以做到这一点。它并没有像预期的那样工作。非常感谢。
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)
}
}