【问题标题】:iOS - UIView does not display inside UIStackViewiOS - UIView 不显示在 UIStackView 内
【发布时间】:2016-08-10 18:38:51
【问题描述】:

我有一个UIStackView,它需要在其中一个堆栈中添加背景颜色,所以我在其中放置了一个UIView,但UIView 从未显示。 UIView 当前有一个子视图UILabel,但最终应该有另一个UIStackView。如果我将UILabel 显示为UIStackView 的直接子代,而不是UIView,则UILabel 会正确显示。那么,我的UIView 缺少哪些约束或错误?

                        let stack = UIStackView()
                        stack.axis = .Vertical
                        stack.alignment = .Leading
                        stack.distribution = .EqualCentering
                        stack.spacing = 0

                        let width = UIScreen.mainScreen().bounds.size.width

                        let frame = CGRect(x: 0, y: 0, width: width, height: 50)
                        let viewHolder = UIView(frame: frame)
                        viewHolder.backgroundColor = blueColor

                        //Needs a blue background
                        let name = UILabel()
                        name.text = nameText
                        name.textColor = yellowColor
                        name.backgroundColor = blueColor
                        name.font = UIFont.boldSystemFontOfSize(32.0)                        

                        let address = UILabel()
                        address.text = addressText

                        let dateTime = UILabel()
                        dateTime.text = calString

                        viewHolder.addSubview(name)
                        stack.addArrangedSubview(viewHolder)
                        stack.addArrangedSubview(address)
                        stack.addArrangedSubview(dateTime)

                        self.stackView.addArrangedSubview(stack)

运行时会产生这样的结果:

如果我从等式中删除 UIView,即:

                        let stack = UIStackView()
                        stack.axis = .Vertical
                        stack.alignment = .Leading
                        stack.distribution = .EqualCentering
                        stack.spacing = 0

                        let width = UIScreen.mainScreen().bounds.size.width

                        let frame = CGRect(x: 0, y: 0, width: width, height: 50)
                        let viewHolder = UIView(frame: frame)
                        viewHolder.backgroundColor = blueColor

                        //Needs a blue background
                        let name = UILabel()
                        name.text = nameText
                        name.textColor = yellowColor
                        name.backgroundColor = blueColor
                        name.font = UIFont.boldSystemFontOfSize(32.0)                        

                        let address = UILabel()
                        address.text = addressText

                        let dateTime = UILabel()
                        dateTime.text = calString

                        //viewHolder.addSubview(name)
                        stack.addArrangedSubview(name) //Add the label directly to the UIStackView
                        stack.addArrangedSubview(address)
                        stack.addArrangedSubview(dateTime)

                        self.stackView.addArrangedSubview(stack)

我得到:

我只需要蓝色部分在屏幕上伸展

【问题讨论】:

    标签: ios iphone swift uiview uistackview


    【解决方案1】:

    所以,答案是,不要使用 UIStackView。使用内置的 UITableView 进行这种布局。

    【讨论】:

    • 您需要更新 UIView 的 intrinsicContentSize 以便 stackView 在水平拉伸之前知道它是初始帧。我猜你的 UIView 的 intrinsicContentSize 被返回为 {0,0} 导致这种行为。
    猜你喜欢
    • 1970-01-01
    • 2020-01-23
    • 2019-01-26
    • 2015-12-15
    • 1970-01-01
    • 1970-01-01
    • 2016-08-22
    相关资源
    最近更新 更多