繁体   English   中英

如何在UIStackView中排列视图?

[英]How to arrange views in the `UIStackView`?

我想在UIStackView使用水平方向设置四个视图。 每个旁边的四个视图没有空间。

在此处输入图片说明

我的代码:

let arrangeStackView = UIStackView()
let followUpActionView = UIView()
let developCurriculumView = UIView()
let moreMoreView = UIView()
let takeCareSalonView = UIView()

override func viewDidLoad() {
    super.viewDidLoad()

    view.addSubview(arrangeStackView)

    developCurriculumView.translatesAutoresizingMaskIntoConstraints = false;
    followUpActionView.translatesAutoresizingMaskIntoConstraints = false;
    takeCareSalonView.translatesAutoresizingMaskIntoConstraints = false;
    moreMoreView.translatesAutoresizingMaskIntoConstraints = false;

    developCurriculumView.backgroundColor = UIColor(red: CGFloat(arc4random()%256)/255.0, green: CGFloat(arc4random()%256)/255.0, blue: CGFloat(arc4random()%256)/255.0, alpha: 1)
    followUpActionView.backgroundColor = UIColor(red: CGFloat(arc4random()%256)/255.0, green: CGFloat(arc4random()%256)/255.0, blue: CGFloat(arc4random()%256)/255.0, alpha: 1)
    takeCareSalonView.backgroundColor = UIColor(red: CGFloat(arc4random()%256)/255.0, green: CGFloat(arc4random()%256)/255.0, blue: CGFloat(arc4random()%256)/255.0, alpha: 1)
    moreMoreView.backgroundColor = UIColor(red: CGFloat(arc4random()%256)/255.0, green: CGFloat(arc4random()%256)/255.0, blue: CGFloat(arc4random()%256)/255.0, alpha: 1)


    arrangeStackView.axis = .horizontal
    arrangeStackView.distribution = .fillEqually

    arrangeStackView.spacing = 10
    arrangeStackView.alignment = .fill

    arrangeStackView.translatesAutoresizingMaskIntoConstraints = false
    arrangeStackView.addSubview(followUpActionView)
    arrangeStackView.addSubview(developCurriculumView)
    arrangeStackView.addSubview(moreMoreView)
    arrangeStackView.addSubview(takeCareSalonView)


    arrangeStackView.snp.makeConstraints { (make) in
        make.center.equalToSuperview()
        make.height.equalTo(95)
        make.width.equalToSuperview()
    }

    let yaIconWith = self.view.frame.width * 0.25

    followUpActionView.snp.makeConstraints{(make) -> Void in
        make.height.equalToSuperview()
        make.width.equalTo(yaIconWith)
    }
    takeCareSalonView.snp.makeConstraints{(make) -> Void in
        make.height.equalToSuperview()
        make.width.equalTo(yaIconWith)
    }
    moreMoreView.snp.makeConstraints{(make) -> Void in
        make.height.equalToSuperview()
        make.width.equalTo(yaIconWith)
    }
    developCurriculumView.snp.makeConstraints{(make) -> Void in
        make.height.equalToSuperview()
        make.width.equalTo(yaIconWith)
    }

现在,我看结果如下。

在此处输入图片说明

为什么这四个视图位于屏幕的左上方?

一个UIStackView只销售其arrangedSubViews没有定期子视图 使用addArrangedSubview而不是addSubView。

您将每个视图作为子视图添加到了堆栈视图,而不是将每个视图作为已安排的子视图添加了。 虽然它们听起来很相似,但仅根据堆栈视图的属性来布置已排列的子视图。

因此,您将更改以下行:

arrangeStackView.addSubview(followUpActionView)
arrangeStackView.addSubview(developCurriculumView)
arrangeStackView.addSubview(moreMoreView)
arrangeStackView.addSubview(takeCareSalonView)

对此:

arrangeStackView.addArrangedSubview(followUpActionView)
arrangeStackView.addArrangedSubview(developCurriculumView)
arrangeStackView.addArrangedSubview(moreMoreView)
arrangeStackView.addArrangedSubview(takeCareSalonView)

并且,如果您需要特殊的安排,或者需要在特定位置的不同时间添加视图,则可以使用insertArrangedSubview(_:at:)替代addArrangedSubview(_:)

暂无
暂无

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

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