我正在尝试创建一个内部包含自定义 View 的堆栈 View 。此示例中的每个自定义 View 都具有相同的信息。出于某种原因,而不是在每个 subview 重叠之后都去,我不知道为什么。我没有任何限制(在布局中)。堆栈 View 在 ScrollView 内,因为可能有很多 subview 。
自定义 View 代码:
import UIKit
class AddressComponentView: UIView {
@IBOutlet weak var labelStreet: UILabel!
@IBOutlet weak var labelCity: UILabel!
@IBOutlet weak var labelOfficialRegionType: UILabel!
@IBOutlet weak var labelOfficialRegionTitle: UILabel!
@IBOutlet weak var labelCountry: UILabel!
@IBOutlet weak var labelZip: UILabel!
var view: UIView!
func loadData(address: AddressData){
self.labelStreet.text = address.street
self.labelCity.text = address.city
self.labelOfficialRegionType.text = address.region?.officialRegionType
self.labelOfficialRegionTitle.text = address.region?.officialRegionTitle
self.labelZip.text = address.zip
self.labelStreet.sizeToFit()
}
override init(frame: CGRect) {
super.init(frame: frame)
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
下面是我如何动态添加 subview (viewContent 是堆栈 View ):
viewScroll.contentSize = CGSize(width: 320, height: 1000)
let viewsDictionary = ["stackView":viewContent]
let stackView_H = NSLayoutConstraint.constraints(
withVisualFormat: "H:|-20-[stackView]-20-|", //horizontal constraint 20 points from left and right side
options: NSLayoutFormatOptions(rawValue: 0),
metrics: nil,
views: viewsDictionary)
let stackView_V = NSLayoutConstraint.constraints(
withVisualFormat: "V:|-30-[stackView]-30-|", //vertical constraint 30 points from top and bottom
options: NSLayoutFormatOptions(rawValue:0),
metrics: nil,
views: viewsDictionary)
view.addConstraints(stackView_H)
view.addConstraints(stackView_V)
let addressView = UINib(nibName: "AddressComponent", bundle: nil).instantiate(withOwner: nil, options: nil)[0] as! AddressComponentView
addressView.loadData(address: (item?.addressData)!)
viewContent.addArrangedSubview(addressView)
let addressView1 = UINib(nibName: "AddressComponent", bundle: nil).instantiate(withOwner: nil, options: nil)[0] as! AddressComponentView
addressView1.loadData(地址:(项目?.addressData)!) viewContent.addArrangedSubview(addressView1)
结果很糟糕:
如果我添加 30 个 subview ,就会发生这种情况:
另一件非常奇怪的事情是,我将背景自定义 View 设为蓝色,并将其设置为不透明,并且由于某种原因这些属性被忽略了。这就是组件在设计时的样子: 现在我尝试将这些 subview 添加到 UITableView 的原型(prototype)单元格中:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.row == 3 || indexPath.row == 7 {
var text = "Run the app again, and it will look nothing has really changed. You are now using your bioLabel, but it’s just showing one line of text in each cell. Even though the number of lines is set to 0 and your constraints are properly configured so your bioLabel takes up"
let customView1 = UINib(nibName: "CustomView1", bundle: nil).instantiate(withOwner: nil, options: nil)[0] as! CustomView1
customView1.frame = CGRect(x:0, y:0, width: 200, height: 60)
customView1.loadData(text: text)
cell.container.addSubview(customView1)
} else {
let customView2 = UINib(nibName: "CustomView2", bundle: nil).instantiate(withOwner: nil, options: nil)[0] as! CustomView2
customView2.loadData(text: "aaaaa adfsd")
cell.container.addSubview(customView2)
}
// Configure the cell...
return cell
}
这段代码非常愚蠢,我只是尝试了不同的 View (我创建了 2 个自定义 View )。它们都有大致相同的代码:
class CustomView1: UIView {
@IBOutlet weak var label1: UILabel!
func loadData(text: String){
label1.text = text
}
override init(frame: CGRect) {
super.init(frame: frame)
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
NB cell.container 是我放入 Prototype cell 的 View 。
最佳答案
问题是 UIStackView 行不知道 UIStackView 的高度,尝试设置该 View 的高度或者可能更改 UIStackView 的分布属性。 为什么你现在为此使用 UITableView,这看起来是使用 UITableView 的完美示例,而不是你不需要 UIScrollView?
关于ios - 为什么堆栈 View 中的自定义 View 重叠?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41072646/