调整UIView的大小限制.迅速 [英] Resize constraints for UIView. swift
问题描述
我在屏幕上有两个自定义UIView.UIViewOne占据了75%的屏幕,UIViewTwo占据了25%的屏幕.我需要单击UIViewTwo,调整它的大小以使其第二个越来越大.我也知道这需要使用约束来完成,但是我不知道如何做.请告诉我如何解决这个问题.
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.
推荐答案
一种方法是向 view1
添加两个高度限制并更改其优先级,
One approach is to add two Height constraints to view1
and change their Priority,
- 将高度限制添加为75%(
乘数= 0.75
) - 将其优先级设置为
999
- 将高度限制添加为25%(
乘数= 0.25
) - 将其优先级设置为
998
- 将
view2
的顶部约束到view1
的底部.
- 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 ofview1
.
开始时,高度限制为75%优先于25%限制... 999
大于 998
.当您点击 view2
时,将75%约束的优先级"更改为 997
.现在 997
小于 998
,因此25%的约束获得了优先级.
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.
由于 view2
的顶部被限制在 view1
的底部,所以 view2
会自动调整大小.
Since view2
's top is constrained to view1
's bottom, view2
will automatically resize.
以下是您可以按原样运行的示例(只需将其分配给视图控制器...就不需要 IBOutlet
或 IBAction
连接):
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()
}
}
}
这篇关于调整UIView的大小限制.迅速的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!