From 9c9e0429237bc2146b05ce7fc722b3be1f250080 Mon Sep 17 00:00:00 2001 From: Kiryl Kaveryn Date: Thu, 30 Nov 2023 15:20:19 +0400 Subject: [PATCH] [ios] refactor: replace the visualFormat constraints system with the anchors Signed-off-by: Kiryl Kaveryn --- .../Classes/CustomAlert/Toast/Toast.swift | 58 +++++++++---------- 1 file changed, 29 insertions(+), 29 deletions(-) diff --git a/iphone/Maps/Classes/CustomAlert/Toast/Toast.swift b/iphone/Maps/Classes/CustomAlert/Toast/Toast.swift index c83e7b94fd..751fc9c39f 100644 --- a/iphone/Maps/Classes/CustomAlert/Toast/Toast.swift +++ b/iphone/Maps/Classes/CustomAlert/Toast/Toast.swift @@ -23,19 +23,16 @@ final class Toast: NSObject { label.numberOfLines = 0 label.font = .regular14() label.textColor = .white - label.frame = blurView.contentView.bounds label.translatesAutoresizingMaskIntoConstraints = false blurView.contentView.addSubview(label) blurView.isUserInteractionEnabled = false - let views = ["label" : label] - blurView.contentView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-8-[label]-8-|", - options: [], - metrics: [:], - views: views)) - blurView.contentView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-8-[label]-8-|", - options: [], - metrics: [:], - views: views)) + + NSLayoutConstraint.activate([ + label.leadingAnchor.constraint(equalTo: blurView.contentView.leadingAnchor, constant: 8), + label.trailingAnchor.constraint(equalTo: blurView.contentView.trailingAnchor, constant: -8), + label.topAnchor.constraint(equalTo: blurView.contentView.topAnchor, constant: 8), + label.bottomAnchor.constraint(equalTo: blurView.contentView.bottomAnchor, constant: -8) + ]) } @objc func show() { @@ -47,26 +44,29 @@ final class Toast: NSObject { } @objc func show(in view: UIView?, alignment: Alignment) { - guard let v = view else { return } + guard let view else { return } blurView.translatesAutoresizingMaskIntoConstraints = false - v.addSubview(blurView) - let views = ["bv" : blurView] - v.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|->=16-[bv]->=16-|", - options: [], - metrics: [:], - views: views)) - let formatString = alignment == .bottom ? "V:[bv]-50-|" : "V:|-50-[bv]" - v.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: formatString, - options: [], - metrics: [:], - views: views)) - v.addConstraint(NSLayoutConstraint(item: blurView, - attribute: .centerX, - relatedBy: .equal, - toItem: v, - attribute: .centerX, - multiplier: 1, - constant: 0)) + view.addSubview(blurView) + + let leadingConstraint = blurView.leadingAnchor.constraint(greaterThanOrEqualTo: view.leadingAnchor, constant: 16) + let trailingConstraint = blurView.trailingAnchor.constraint(lessThanOrEqualTo: view.trailingAnchor, constant: -16) + + NSLayoutConstraint.activate([ + leadingConstraint, + trailingConstraint + ]) + + let topConstraint: NSLayoutConstraint + if alignment == .bottom { + topConstraint = blurView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -50) + } else { + topConstraint = blurView.topAnchor.constraint(equalTo: view.topAnchor, constant: 50) + } + + NSLayoutConstraint.activate([ + topConstraint, + blurView.centerXAnchor.constraint(equalTo: view.safeAreaLayoutGuide.centerXAnchor) + ]) UIView.animate(withDuration: kDefaultAnimationDuration) { self.blurView.alpha = 1