我已按照此答案從 XIB 檔案實體化視圖:
extension UIView {
class func fromNib(named: String? = nil) -> Self {
let name = named ?? "\(Self.self)"
guard let nib = Bundle.main.loadNibNamed(name, owner: nil, options: nil) else {
fatalError("missing expected nib named: \(name)")
}
guard let view = nib.first as? Self else {
fatalError("view of type \(Self.self) not found in \(nib)")
}
return view
}
}
現在我想根據其內容創建一個具有自適應高度的視圖:
class CustomTopView: UIView {
@IBOutlet weak var titleLabel: UILabel!
@IBOutlet weak var subtitleLabel: UILabel!
static func instance(withTitle title: NSAttributedString, subtitle: NSAttributedString) -> CustomTopView {
let customTopView = CustomTopView.fromNib()
customTopView.titleLabel.attributedText = title
customTopView.subtitleLabel.attributedText = subtitle
return customTopView
}
}
我想要做的是從螢屏頂部影片顯示此視圖,就像通知彈出視窗一樣。我是這樣寫的:
extension UIView {
func showCustomTopView(withTitle title: NSAttributedString, subtitle: NSAttributedString) {
let customTopView = CustomTopView.instance(withTitle: title, subtitle: subtitle)
addSubview(customTopView)
customTopView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
customTopView.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 20.0),
customTopView.constraint(equalTo: customTopView.trailingAnchor, constant: 20.0),
])
customTopView.layoutIfNeeded() // *
NSLayoutConstraint.activate([customTopView.topAnchor.constraint(equalTo: topAnchor, constant: -customTopView.bounds.height)])
}
}
我這里有兩個問題。主要的一點是,我不知道如何從這里開始執行我想要的影片,以便視圖最終可見。我在最后一次之后嘗試了這個NSLayoutConstraint.activate(...):
UIView.animate(withDuration: 0.5) {
self.topAnchor.constraint(equalTo: customTopView.topAnchor, constant: 100.0).isActive = true
self.layoutIfNeeded()
}
但不是按預期顯示,彈出視窗從螢屏頂部開始 100 像素,然后上升并消失。我究竟做錯了什么?
另外,我不確定標有// *: 我寫這一行來獲取正確customTopView高度的行,但我不確定這是正確的方法。
感謝您的幫助!
uj5u.com熱心網友回復:
一種方法:
- 在視圖頂部上方創建一個約束設定底部
customTopView - 創建第二個約束,設定
customTopView視圖頂部下方的頂部 - 添加
customTopView為子視圖,激活第一個約束 customTopView通過停用第一個約束并激活第二個約束來制作影片
這是您的showCustomTopView(...)擴展的修改版本,使用UILabelas customTopView-- 應該適用于您的CustomTopView.instance(withTitle: ...):
extension UIView {
func showCustomTopView(withTitle title: NSAttributedString, subtitle: NSAttributedString) {
let customTopView = UILabel()
customTopView.text = title.string
customTopView.backgroundColor = .green
addSubview(customTopView)
customTopView.translatesAutoresizingMaskIntoConstraints = false
let g = self.safeAreaLayoutGuide
// constraint: Bottom of customTopView to Top of self Minus 8-pts
let cvBot = customTopView.bottomAnchor.constraint(equalTo: topAnchor, constant: -8.0)
// constraint: Top of customTopView to Top of self Plus 8-pts
let cvTop = customTopView.topAnchor.constraint(equalTo: g.topAnchor, constant: 8.0)
NSLayoutConstraint.activate([
customTopView.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 20.0),
customTopView.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -20.0),
// activate cvBot, so customTopView is 8-pts above the top of self
cvBot,
])
// execute this async, so the initial view position is set
DispatchQueue.main.async {
// deactivate the Bottom constraint
cvBot.isActive = false
// activate the Top constraint
cvTop.isActive = true
// animate it into view
UIView.animate(withDuration: 0.5, animations: {
self.layoutIfNeeded()
})
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/346115.html
標籤:ios 工具包 nslayout约束
