ios - 为什么堆栈 View 中的自定义 View 重叠?

标签 ios swift stackview

我正在尝试创建一个内部包含自定义 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)

结果很糟糕:

crap

That's what happened if I add 30 subviews:

enter image description here

如果我添加 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/

相关文章:

ios - UITableView - 如何在滚动时不转储/重新加载单元格

iphone - 如何限制 UILabel 文本长度 - IOS

ios - 在没有 segue 的情况下在多个 View Controller 之间共享数据(Swift 3)

ios - 如何在不阻塞 UI 的情况下创建 UIView

objective-c - 导致 viewDidLoad 运行

ios - 来电时保持 callkit 通话

ios - Swift:点击手势无法识别

ios - StackView 未以编程方式左对齐

ios - iOS 中的键盘处理

ios - 具有默认参数的 swift 函数也是一个选择器?