首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使堆栈视图元素居中,而不填充它们

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

Stack Overflow用户
提问于 2019-03-22 07:37:38
回答 1查看 53关注 0票数 0

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

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

以下是一些截图:

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

代码语言:javascript
运行
复制
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
    }

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-03-22 10:23:45

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55290789

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档