中心堆栈视图元素,而不是填充它们

问题描述 投票:0回答:1

我使用UIStackView作为UITableView's BackGroundView属性,所以当获取填充tableView的集合时出错,我可以调用一个显示此堆栈视图的函数,其中包含显示警告消息和重试按钮的视图。

我测试了在一个空的UIViewController做类似的行为,所以我可以把stackView和它的孩子集中。当我将堆栈视图固定到superView的尾随和前导时,解决方案工作,垂直居中并将其顶部锚定设置为大于或等于superView的顶部锚点,同样它的底部锚点大于或等于superView的底部锚点。我还将对齐设置为中心和分布以填充,所有似乎都正常工作。

以下是一些截图:enter image description here

enter image description here

enter image description here

我在UITableView的扩展中使用了这段代码,但只能重现这种行为。这段代码有什么错误吗?

func show(error: Bool, withMessage message : String? = nil, andRetryAction retry: (() -> Void)? = nil){
    if error{
        let iconLabel = UILabel()
        iconLabel.GMDIcon = .gmdErrorOutline
        iconLabel.textAlignment = .center
        iconLabel.numberOfLines = 0
        iconLabel.font = iconLabel.font.withSize(50)
        iconLabel.textColor = Constants.Colors.ErrorColor

        iconLabel.backgroundColor = .blue

        let messageLabel = UILabel()
        messageLabel.text = message ?? "Ocorreu um erro"
        messageLabel.textColor = Constants.Colors.ErrorColor
        messageLabel.numberOfLines = 0
        messageLabel.textAlignment = .center
        messageLabel.font = UIFont(name: "TrebuchetMS", size: 20)

        messageLabel.backgroundColor = .green

        var views: [UIView] = [iconLabel, messageLabel]

        if let retry = retry{
            let button = RaisedButton(title: "Tentar novamente")
            button.pulseColor = Constants.Colors.PrimaryTextColor
            button.backgroundColor = Constants.Colors.PrimaryColor
            button.titleColor = .white
            button.actionHandle(controlEvents: .touchUpInside, ForAction: retry)
            button.contentEdgeInsets = UIEdgeInsetsMake(10,10,10,10)


            views.append(button)
        }


    }else{
        self.backgroundView = nil
    }
}
let stack = UIStackView()
    stack.spacing = 10
    stack.axis = .vertical
    stack.alignment = .center
    stack.distribution = .fill
    stack.translatesAutoresizingMaskIntoConstraints = false


    for view in views{
        view.translatesAutoresizingMaskIntoConstraints = false
        stack.addArrangedSubview(view)
    }

    if self.tableFooterView == nil{
        tableFooterView = UIView()
    }
    self.backgroundView = stack;

    if #available(iOS 11, *) {
        let guide = self.safeAreaLayoutGuide

        stack.topAnchor.constraint(greaterThanOrEqualTo: guide.topAnchor).isActive = true
        stack.bottomAnchor.constraint(greaterThanOrEqualTo: guide.bottomAnchor).isActive = true
        stack.leadingAnchor.constraint(equalTo: guide.leadingAnchor).isActive = true
        stack.trailingAnchor.constraint(equalTo: guide.trailingAnchor).isActive = true
        stack.centerYAnchor.constraint(equalTo: guide.centerYAnchor).isActive = true
    } else {
        stack.topAnchor.constraint(greaterThanOrEqualTo: self.topAnchor).isActive = true
        stack.bottomAnchor.constraint(greaterThanOrEqualTo: self.bottomAnchor).isActive = true
        stack.leadingAnchor.constraint(equalTo: self.leadingAnchor).isActive = true
        stack.trailingAnchor.constraint(equalTo: self.trailingAnchor).isActive = true
        stack.centerYAnchor.constraint(equalTo: self.centerYAnchor).isActive = true
    }

enter image description here

ios swift iphone uitableview uistackview
1个回答
1
投票

如果堆栈视图的元素都具有内在大小(即,它们各自高度的总和+项目间距),则堆栈视图知道其高度。在这种情况下,因为你有2 y的位置约束,你暗示一个高度,所以你的约束是不可满足的。您需要的唯一y轴约束是垂直居中。摆脱顶部和底部的限制。然后,系统将使用内在大小来计算堆栈视图的高度,并在背景视图中将其垂直居中。保持x轴的约束。

© www.soinside.com 2019 - 2024. All rights reserved.