簡體   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