简体   繁体   English

调整 UIView 的大小限制。 迅速

[英]Resize constraints for UIView. swift

I have two custom UIViews on the screen.我在屏幕上有两个自定义 UIView。 UIViewOne occupies 75% of the screen, and UIViewTwo 25%. UIViewOne 占屏幕的 75%,UIViewTwo 占 25%。 I need to click on UIViewTwo, resize it to make the second bigger and smaller first.我需要单击 UIViewTwo,调整它的大小以使第二个先变大和变小。 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.请告诉我如何解决这个问题。

For both view1 and view2,对于视图 1 和视图 2,

  1. add constraint as equal height to superview将约束作为等高添加到超级视图
  2. update the height constraint multiplier to 0.75 for view1 and 0.25 for view2.将视图 1 的高度约束乘数更新为 0.75,将视图 2 更新为 0.25。

When you click on view2, similarly update the height constraint multiplier to 0.25 for view1 and 0.75 for view2.当您单击 view2 时,同样将高度约束乘数更新为 view1 的 0.25 和 view2 的 0.75。

One approach is to add two Height constraints to view1 and change their Priority ,一种方法是向view1添加两个Height 约束并更改它们的Priority

  • add a Height constraint at 75% ( multiplier = 0.75 )在 75% 处添加高度约束( multiplier = 0.75
  • set its Priority to 999将其优先级设置为999
  • add a Height constraint at 25% ( multiplier = 0.25 )在 25% 处添加高度约束( multiplier = 0.25
  • set its Priority to 998将其优先级设置为998
  • constraint the Top of view2 to the bottom of view1 .view2的顶部约束到view1的底部。

At the start, the 75% Height constraint will have priority over the 25% constraint ... 999 is greater than 998 .一开始, 75% 高度约束将优先于 25% 约束...... 999大于998 When you tap view2 , change the 75% constraint's Priority to 997 .当您点击view2 ,将 75% 约束的 Priority 更改为997 Now 997 is less than 998 , so the 25% constraint gets the priority.现在997小于998 ,因此 25% 约束获得优先权。

Since view2 's top is constrained to view1 's bottom, view2 will automatically resize.由于view2的顶部被限制在view1的底部,因此view2将自动调整大小。

Here is an example you can run as-is (just assign it to a view controller... no IBOutlet or IBAction connections needed):这是一个您可以按原样运行的示例(只需将其分配给视图控制器...不需要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