将堆栈视图元素居中,而不填充它们

时间:2019-03-21 23:37:38

标签: ios swift iphone uitableview uistackview

我正在将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

1 个答案:

答案 0 :(得分:1)

如果堆栈视图的所有元素均具有固有尺寸(即其各个高度的总和+中间间距),则堆栈视图会知道其高度。在这种情况下,因为您有2 y个位置约束,所以您暗示的是高度,因此您的约束无法满足。您唯一需要的y轴约束是垂直居中。摆脱最高和最低约束。然后,系统将使用固有尺寸来计算堆栈视图的高度,并将其在背景视图中垂直居中。保持x轴约束不变。