簡體   English   中英

調整 UIView 的大小限制。 迅速

[英]Resize constraints for UIView. swift

我在屏幕上有兩個自定義 UIView。 UIViewOne 占屏幕的 75%,UIViewTwo 占 25%。 我需要單擊 UIViewTwo,調整它的大小以使第二個先變大和變小。 我也知道這需要使用約束來完成,但我不知道如何。 請告訴我如何解決這個問題。

對於視圖 1 和視圖 2,

  1. 將約束作為等高添加到超級視圖
  2. 將視圖 1 的高度約束乘數更新為 0.75,將視圖 2 更新為 0.25。

當您單擊 view2 時,同樣將高度約束乘數更新為 view1 的 0.25 和 view2 的 0.75。

一種方法是向view1添加兩個Height 約束並更改它們的Priority

  • 在 75% 處添加高度約束( multiplier = 0.75
  • 將其優先級設置為999
  • 在 25% 處添加高度約束( multiplier = 0.25
  • 將其優先級設置為998
  • view2的頂部約束到view1的底部。

一開始, 75% 高度約束將優先於 25% 約束...... 999大於998 當您點擊view2 ,將 75% 約束的 Priority 更改為997 現在997小於998 ,因此 25% 約束獲得優先權。

由於view2的頂部被限制在view1的底部,因此view2將自動調整大小。

這是一個您可以按原樣運行的示例(只需將其分配給視圖控制器...不需要IBOutletIBAction連接):

class PercentViewController: UIViewController {

    let view1: UIView = {
        let v = UIView()
        v.backgroundColor = .red
        return v
    }()

    let view2: UIView = {
        let v = UIView()
        v.backgroundColor = .green
        return v
    }()

    var topView75: NSLayoutConstraint!
    var topView25: NSLayoutConstraint!

    override func viewDidLoad() {
        super.viewDidLoad()

        // we're using auto-layout
        view1.translatesAutoresizingMaskIntoConstraints = false
        view2.translatesAutoresizingMaskIntoConstraints = false

        // add views
        view.addSubview(view1)
        view.addSubview(view2)

        // respect safe area
        let g = view.safeAreaLayoutGuide

        // create "75% height constraint"
        topView75 = view1.heightAnchor.constraint(equalTo: g.heightAnchor, multiplier: 0.75)
        // create "25% height constraint"
        topView25 = view1.heightAnchor.constraint(equalTo: g.heightAnchor, multiplier: 0.25)

        // give 75% constraint higher priority than 25% constraint
        topView75.priority = UILayoutPriority(rawValue: 999)
        topView25.priority = UILayoutPriority(rawValue: 998)

        NSLayoutConstraint.activate([

            // view1 constrained Top, Leading, Trailing (to safe-area)
            view1.topAnchor.constraint(equalTo: g.topAnchor),
            view1.leadingAnchor.constraint(equalTo: g.leadingAnchor),
            view1.trailingAnchor.constraint(equalTo: g.trailingAnchor),

            // view2 constrained Bottom, Leading, Trailing (to safe-area)
            view2.bottomAnchor.constraint(equalTo: g.bottomAnchor),
            view2.leadingAnchor.constraint(equalTo: g.leadingAnchor),
            view2.trailingAnchor.constraint(equalTo: g.trailingAnchor),

            // view2 Top constrained to view1 Bottom
            view2.topAnchor.constraint(equalTo: view1.bottomAnchor),

            // activate both Height constraints
            topView75,
            topView25,

        ])

        // create tap gesture recognizers
        let tap1 = UITapGestureRecognizer(target: self, action: #selector(view1Tapped(_:)))
        let tap2 = UITapGestureRecognizer(target: self, action: #selector(view2Tapped(_:)))

        // add to the views
        view1.addGestureRecognizer(tap1)
        view2.addGestureRecognizer(tap2)

    }

    @objc func view1Tapped(_ sender: Any) -> Void {
        // view1 tapped, so give 75% constraint a higher priority than 25% constraint
        topView75.priority = UILayoutPriority(rawValue: 999)
        // 0.3-second animation
        UIView.animate(withDuration: 0.3) {
            self.view.layoutIfNeeded()
        }
    }

    @objc func view2Tapped(_ sender: Any) -> Void {
        // view2 tapped, so give 25% constraint a higher priority than 75% constraint
        topView75.priority = UILayoutPriority(rawValue: 997)
        // 0.3-second animation
        UIView.animate(withDuration: 0.3) {
            self.view.layoutIfNeeded()
        }
    }

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM