0
我想創建一個基於視圖大小的容器視圖。代碼自身說明,但不起作用。我試過使用覆蓋viewWillAppear無濟於事。NSLayoutConstraints不參考視圖
進口的UIKit
class ViewController: UICollectionViewController, UICollectionViewDelegateFlowLayout, UITextFieldDelegate {
let searchBarContainer: UIView = {
let sBarContainer = UIView()
sBarContainer.backgroundColor = UIColor.gray
return sBarContainer
}()
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.title = "Search Bar"
collectionView?.backgroundColor = UIColor.white
view.addSubview(searchBarContainer)
NSLayoutConstraint(item: searchBarContainer, attribute: .width, relatedBy: .equal, toItem: view, attribute: .width, multiplier: 1, constant: 0).isActive = true
NSLayoutConstraint(item: searchBarContainer, attribute: .height, relatedBy: .equal, toItem: view, attribute: .height, multiplier: 1, constant: 0).isActive = true
NSLayoutConstraint(item: searchBarContainer, attribute: .bottom, relatedBy: .equal, toItem: view, attribute: .bottom, multiplier: 1, constant: 0).isActive = true
}
}
有什麼建議?謝謝
您沒有任何設置子視圖的x位置 – dan
您還需要將'translatesAutoresizingMaskIntoConstraints'設置爲false – dan