iOS - UIView不会显示在UIStackView中

时间:2016-08-10 18:38:51

标签: ios iphone swift uiview uistackview

我有一个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)

运行时会生成:

No UIView, or UILablel

如果我从等式中移除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)

我得到: UILables showing, but no UIVIew

我只需要将蓝色部分拉伸到屏幕上

1 个答案:

答案 0 :(得分:0)

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