Ios 为什么在添加UIPickerView时,不相关的自动布局约束会发生更改?
将图像的选择器视图添加到视图时,它会将其他对象的自动布局约束更改为选择器视图中未受任何约束的对象 添加选择器视图之前: 添加选择器视图后: 在这里,我添加了mainContainer(UIView,它只覆盖屏幕,应用程序标题所在的顶部除外)和primaryHeaderLabel(UILabel,表示“新建托盘”)。如您所见,这些自动布局约束都不包含imagePickerView中的任何内容,但它们仍在移动中Ios 为什么在添加UIPickerView时,不相关的自动布局约束会发生更改?,ios,swift,autolayout,uipickerview,Ios,Swift,Autolayout,Uipickerview,将图像的选择器视图添加到视图时,它会将其他对象的自动布局约束更改为选择器视图中未受任何约束的对象 添加选择器视图之前: 添加选择器视图后: 在这里,我添加了mainContainer(UIView,它只覆盖屏幕,应用程序标题所在的顶部除外)和primaryHeaderLabel(UILabel,表示“新建托盘”)。如您所见,这些自动布局约束都不包含imagePickerView中的任何内容,但它们仍在移动中 view.addSubview(mainContainer) mainCon
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)
以下是与imagePickerView相关的代码位。我尝试将选择器视图添加到主视图而不是主容器,但得到了相同的结果:
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
选择器视图委托方法和数据源:
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
}
有人能帮我吗?任何帮助都将不胜感激。这里
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.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)
将拾取器的底部设置为使其位于其下方的主标签的底部,因此删除拾取器的底部约束,并为其指定高度约束