Resize constraints for UIView. swift

不羁的心 提交于 2021-01-28 18:21:35

问题


I have two custom UIViews on the screen. UIViewOne occupies 75% of the screen, and UIViewTwo 25%. I need to click on UIViewTwo, resize it to make the second bigger and smaller first. I also know that this needs to be done using constraints, but I don't know how. Please tell me how to solve this problem.


回答1:


One approach is to add two Height constraints to view1 and change their Priority,

  • add a Height constraint at 75% (multiplier = 0.75)
  • set its Priority to 999
  • add a Height constraint at 25% (multiplier = 0.25)
  • set its Priority to 998
  • constraint the Top of view2 to the bottom of view1.

At the start, the 75% Height constraint will have priority over the 25% constraint ... 999 is greater than 998. When you tap view2, change the 75% constraint's Priority to 997. Now 997 is less than 998, so the 25% constraint gets the priority.

Since view2's top is constrained to view1's bottom, view2 will automatically resize.

Here is an example you can run as-is (just assign it to a view controller... no IBOutlet or IBAction connections needed):

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()
        }
    }

}



回答2:


For both view1 and view2,

  1. add constraint as equal height to superview
  2. update the height constraint multiplier to 0.75 for view1 and 0.25 for view2.

When you click on view2, similarly update the height constraint multiplier to 0.25 for view1 and 0.75 for view2.



来源:https://stackoverflow.com/questions/59876808/resize-constraints-for-uiview-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!