gpt4 book ai didi

ios - 如何以编程方式更改锚定样式约束?

转载 作者:行者123 更新时间:2023-11-30 13:20:09 26 4
gpt4 key购买 nike

我进行了搜索,甚至想出了潜在的解决方案。遗憾的是,他们都没有说话。我正在尝试通过增加 View 的leadingAnchor-Constraint来为 View 的x位置设置动画。

我像这样创建约束:

let margins = self.layoutMarginsGuide
horizontalConstraint = underline.leadingAnchor.constraintEqualToAnchor(margins.leadingAnchor)

并像这样编辑约束常量:

func updateUnderlinePosition(offset:CGFloat, pageCount:Int) {

//Underline just over width of one Screen:
var underlineOffset = offset / CGFloat(pageCount)

//Add left margin:
let margins = self.layoutMarginsGuide
underlineOffset += 15

//reposition underlineview
horizontalConstraint.constant += underlineOffset
self.layoutIfNeeded()
}

为什么我会收到错误消息,告诉我存在冲突的约束?

"<NSLayoutConstraint:0x7fca2b8c72c0 UIView:0x7fca29d1c830.leading == UILayoutGuide:0x7fca2b8c4970'UIViewLayoutMarginsGuide'.leading>",
"<NSLayoutConstraint:0x7fca29c2ccd0 UIView:0x7fca29d1c830.leading == UILayoutGuide:0x7fca2b8c4970'UIViewLayoutMarginsGuide'.leading + 16.25>"

编辑:

约束是这样创建的:

override func layoutSubviews() {
super.layoutSubviews()

//underline.frame = CGRectMake(15, self.frame.height-10, 40, 2)
underline.translatesAutoresizingMaskIntoConstraints = false
underline.backgroundColor = UIColor.whiteColor()
underline.layer.cornerRadius = 1.0
self.addSubview(underline)
print(self.backItem)
let margins = self.layoutMarginsGuide
horizontalConstraint = underline.leadingAnchor.constraintEqualToAnchor(margins.leadingAnchor)
let verticalConstraint = underline.centerYAnchor.constraintEqualToAnchor(margins.bottomAnchor)
let widthConstraint = underline.widthAnchor.constraintEqualToAnchor(nil, constant: 40)
let heightConstraint = underline.heightAnchor.constraintEqualToAnchor(nil, constant: 2)
NSLayoutConstraint.activateConstraints([horizontalConstraint, verticalConstraint, widthConstraint, heightConstraint])
}

最佳答案

检查layoutSubviews是否被调用两次

关于ios - 如何以编程方式更改锚定样式约束?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37847530/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com