視圖控制器:
class ViewController: UIViewController, ViewSpecificController {
typealias RootView = CustomView
override func viewDidLoad() {
super.viewDidLoad()
view().configure()
}
override func loadView() { self.view = CustomView() }
}
界面視圖:
class CustomView: UIView {
func configure() {
backgroundColor = .orange
translatesAutoresizingMaskIntoConstraints = false
addConstraints()
}
func addConstraints() {
var constraints = [NSLayoutConstraint]()
constraints.append(self.leadingAnchor.constraint(equalTo: self.safeAreaLayoutGuide.leadingAnchor))
constraints.append(self.trailingAnchor.constraint(equalTo: self.safeAreaLayoutGuide.trailingAnchor))
constraints.append(self.topAnchor.constraint(equalTo: self.safeAreaLayoutGuide.topAnchor))
constraints.append(self.bottomAnchor.constraint(equalTo: self.safeAreaLayoutGuide.bottomAnchor))
NSLayoutConstraint.activate(constraints)
}
}
執行此代碼會導致錯誤“[LayoutConstraints] 無法同時滿足約束。可能以下串列中的至少一個約束是您不想要的。” 我試圖初始化 UIView,同樣的錯誤出現在那里。如何解決?
uj5u.com熱心網友回復:
從我所見,您的 CustomView 類似乎正在嘗試對其自身設定約束。不需要約束,因為一旦你在 loadView() 中替換了原來的,ViewController 會自動處理它的大小。從 configure() 中洗掉您的 addConstraints() 方法應該可以解決您的問題。看看有沒有效果...
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/437380.html
上一篇:無法在swiftui中使用帶有ifelse條件的ToggleSwitch
下一篇:為什么UITableViewCells中的UIButtons會丟失它們在運行時設定的值并默認回傳到它們的InterfaceBuilder值?
