[英]Resize constraints for UIView. swift
我在屏幕上有兩個自定義 UIView。 UIViewOne 占屏幕的 75%,UIViewTwo 占 25%。 我需要單擊 UIViewTwo,調整它的大小以使第二個先變大和變小。 我也知道這需要使用約束來完成,但我不知道如何。 請告訴我如何解決這個問題。
對於視圖 1 和視圖 2,
當您單擊 view2 時,同樣將高度約束乘數更新為 view1 的 0.25 和 view2 的 0.75。
一種方法是向view1
添加兩個Height 約束並更改它們的Priority ,
multiplier = 0.75
)999
multiplier = 0.25
)998
view2
的頂部約束到view1
的底部。 一開始, 75% 高度約束將優先於 25% 約束...... 999
大於998
。 當您點擊view2
,將 75% 約束的 Priority 更改為997
。 現在997
小於998
,因此 25% 約束獲得優先權。
由於view2
的頂部被限制在view1
的底部,因此view2
將自動調整大小。
這是一個您可以按原樣運行的示例(只需將其分配給視圖控制器...不需要IBOutlet
或IBAction
連接):
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.