想知道如何更新自定義UI視圖的約束。在我的代碼中肯定是錯誤的。我提前道歉,我只是Swift的初學者。Swift Playgrounds中的Programmatic Constraint問題程序
public class NoteCardView:UIView {
@IBInspectable var contentView = UIButton(frame: .zero)
@IBInspectable var delegate: MainViewController?
var leftAnchor: NSLayoutXAxisAnchor
var bottomAnchor: NSLayoutYAxisAnchor
override public func updateConstraints() {
contentView.translatesAutoresizingMaskIntoConstraints = false
contentView.layer.masksToBounds = true
contentView.layer.cornerRadius = 6
contentView.widthAnchor.constraint(equalToConstant: 75).isActive = true
contentView.heightAnchor.constraint(equalToConstant: 100).isActive = true
leftAnchor = contentView.leftAnchor.constraint(equalTo: contentView.leftAnchor, constant: positionX).isActive = true
bottomAnchor = contentView.bottomAnchor.constraint(equalTo: (delegate?.view.bottomAnchor)!, constant: positionY).isActive = true
titleLabel.textAlignment = .center
titleLabel.text = note
contentView.addSubview(titleLabel)
super.updateConstraints()
}
class MainViewController: UIViewController {
override func viewDidLoad() {
// Trying to implement updated constraints to the NoteCardView here.
}
}
還有這個與TapGestureRecognizer無關的其他問題。我對這個概念也不是很熟悉。
public class NoteCard:UIView {
internal var titleLabelTapGestureRecognizer: UITapGestureRecognizer?
internal func commonInit() {
self.titleLabelTapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(handleTitleLabelTap(UITapGestureRecognizer)))
}
internal func handleTitleLabelTap(_ recognizer:UITapGestureRecognizer) {
self.delegate?.noteCardViewTitleLabelDidRecieveTap(self)
}
}
預先感謝您的幫助!
如果沒有別的,您的左側錨點位於內容視圖和其本身之間,這是沒有意義的。此外,您通常需要在設置任何約束之前將視圖添加到層次結構中。如果您收到基於約束的錯誤消息,請分享精確的消息(修復上述約束後)。 – Rob