简体   繁体   English

使用swift在UiStackview中设置UiLabel的高度

[英]Setting height of UiLabel inside UiStackview Using swift

I am dynamically adding views in the UiStackview. 我在UiStackview中动态添加视图。 Since UiStackview is not a regular view, SO I can not add a bottom border to it. 由于UiStackview不是常规视图,因此我无法为其添加底部边框。 That is why I have planned to add a UILabel at the end of it 这就是为什么我计划在其末尾添加一个UILabel的原因

The label that I will add at the end of my UIStackview will be dealt as a border. 我将在UIStackview的末尾添加的标签将作为边框处理。 I was thinking to make its height as 1point. 我当时正在考虑使其高度为1点。 and give it a background color. 并为其赋予背景色。 And expand its height to full width of screen. 并将其高度扩展到屏幕的整个宽度。

But its height is not getting controlled. 但是它的高度并没有得到控制。 Can anyone tell me what the problem is? 谁能告诉我问题出在哪里?

Here is the little code snippet 这是小代码片段

let label = UILabel(frame: CGRect(x: 0, y: 0, width: 10, height: 1))
            label.backgroundColor = UIColor.black
            label.text = ""


            bottomBorder.addArrangedSubview(label)

I am adding this in the end of the main stackview. 我将其添加到主stackview的末尾。 it gets added in the main stackview but with the height of I think 30 point. 它被添加到主stackview中,但高度为30点。 Or may be its the default height of the UiLabel 或者可能是UiLabel的默认高度

My questions are: 我的问题是:

  1. How to add UiLabel or a border at the end of stackview (vertical alignment) 如何在堆栈视图的末尾添加UiLabel或边框(垂直对齐)
  2. Is there any way that I can add border to my stackview directly? 有什么办法可以直接在我的stackview中添加边框? Four side border or at least bottom border? 四个侧面边框还是至少底部边框?

You have 2 alternatives to achieve this: 您有2种替代方法可以实现此目的:

1- put the stackview inside a UIView parentView 1-将stackview放在UIView parentView

在此处输入图片说明

2- do not add UILabel directly to the UIStackView , add the UILabel to UIView then add the UIView to the UIStackView , so that you can whatever separators you want to the UIView . 2 -不添加UILabel直接向UIStackView ,添加UILabelUIView那么添加UIViewUIStackView ,这样你就可以想的任何分离UIView

Code to do that: 代码来做到这一点:

override func viewDidLoad()
    {
        super.viewDidLoad()
        stackView.distribution = .fillEqually

        let v1 = getViewForStackView(lblText: "lbl1")
        let v2 = getViewForStackView(lblText: "lbl2")

        stackView.addArrangedSubview(v1)
        stackView.addArrangedSubview(v2)
    }


    func getViewForStackView(lblText:String)->UIView
    {
        let rectView = CGRect(x: 0, y: 0, width: 100, height: 100)
        let view = UIView(frame: rectView)
        view.backgroundColor = .green
        let label = UILabel()
        label.text = lblText
        label.backgroundColor = .red
        addFillingSubview(parentView: view, subview: label, insets: UIEdgeInsets(top: 8, left: 8, bottom: 8, right: 8))
        return view
    }

    func addFillingSubview(parentView:UIView, subview: UIView, insets: UIEdgeInsets = .zero)
    {
        subview.translatesAutoresizingMaskIntoConstraints = false
        parentView.addSubview(subview)

        let views = ["subview": subview]
        let metrics = ["top": insets.top, "left": insets.left, "bottom": insets.bottom, "right": insets.right]

        parentView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "|-(left)-[subview]-(right)-|", options: [], metrics: metrics, views: views))
        parentView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-(top)-[subview]-(bottom)-|", options: [], metrics: metrics, views: views))
    }

Output: 输出:

在此处输入图片说明

Use this class 使用这个课程

class BorderedStackView: UIStackView {
    let borderWidth : Int = 2
    let borderColor : UIColor = UIColor.darkGray;
    var borderView : UIView!


    required init(coder: NSCoder) {
        super.init(coder: coder);
        initializeSubviews();
    }

    required override init(frame: CGRect) {
        super.init(frame: frame);
        initializeSubviews();
    }

    func initializeSubviews() {
        borderView = UIView.init();
        borderView.backgroundColor = UIColor.black;
        self.addSubview(borderView);
    }

    override func layoutSubviews() {
        super.layoutSubviews();

        var frame = self.bounds;
        frame.origin.y = frame.size.height - CGFloat.init(borderWidth)
        frame.size.height = CGFloat.init(borderWidth);
        self.borderView.frame = frame;
        self.bringSubview(toFront: self.borderView)
    }
}

Uses: 用途:

    let stackView = BorderedStackView.init(frame: CGRect.init(x: 50, y: 50, width: 200, height: 200));
    stackView.axis = .vertical
    stackView.borderWidth = 20;
    stackView.borderColor = .red;
    self.view.addSubview(stackView);

    let subView1 = UIView.init(frame: CGRect.init(x: 0, y: 0, width: 200, height: 100));
    subView1.backgroundColor = UIColor.red;
    stackView.addSubview(subView1);

    let subView2 = UIView.init(frame: CGRect.init(x: 0, y: 100, width: 200, height: 100));
    subView2.backgroundColor = UIColor.black;
    stackView.addSubview(subView2);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM