When I add my picker view of images to the view, it changes the autolayout constraints of other objects that are not constrained in any way to the picker view.
Before adding the picker view:
Here, I'm adding the mainContainer (UIView that just covers the screen except for the top where the app title is) and primaryHeaderLabel (UILabel that says "NEW TRAY"). As you can see, none of those autolayout constraints contain anything from the imagePickerView, but they're still being moved.
view.addSubview(mainContainer)
mainContainer.setAnchors(top: titleLabel.bottomAnchor, paddingTop: 5, bottom: view.bottomAnchor, paddingBottom: 0, left: view.leftAnchor, paddingLeft: 0, right: view.rightAnchor, paddingRight: 0, centerX: nil, centerY: nil, width: 0, height: 0)
mainContainer.addSubview(primaryHeaderLabel)
primaryHeaderLabel.setAnchors(top: mainContainer.topAnchor, paddingTop: 20, bottom: nil, paddingBottom: 0, left: mainContainer.leftAnchor, paddingLeft: 40, right: mainContainer.rightAnchor, paddingRight: 40, centerX: nil, centerY: nil, width: 0, height: 0)
Here are the code bits related to the imagePickerView. I have tried adding the picker view to the main view instead of the mainContainer, but got the same result:
let imagePickerView: UIPickerView = {
let pickerView = UIPickerView()
return pickerView
}()
let pickerItems: [String] = [
"pickerPink",
"pickerBlue",
"pickerPurple",
"pickerYellow"
]
mainContainer.addSubview(imagePickerView)
imagePickerView.setAnchors(top: primaryHeaderLabel.topAnchor, paddingTop: 0, bottom: primaryHeaderLabel.bottomAnchor, paddingBottom: 0, left: nil, paddingLeft: 0, right: mainContainer.rightAnchor, paddingRight: 40, centerX: nil, centerY: nil, width: 60, height: 0)
imagePickerView.delegate = self
imagePickerView.dataSource = self
Picker view delegate methods and data source:
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return pickerItems.count
}
func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
let view = UIView(frame: CGRect(x: 0, y: 0, width: 30, height: 30))
let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 30, height: 30))
imageView.image = UIImage(named: pickerItems[row])
view.addSubview(imageView)
return view
}
Can someone help me with this? Any help would be greatly appreciated.