UIStackView排列子视图底部排列问题

时间:2019-08-08 13:28:23

标签: ios swift uistackview

enter image description here enter image description here enter image description here

我想从底部对齐开始addArrangedSubview,如所附的第二个屏幕截图(第三个是我的实际工作屏幕截图)。但是每次它从上到下排列。但是我需要从下到上进行排列。我想使用UIScrollView内的UIStackView创建此设计。由于横向支持,我正在尝试使用UIScrollView。和UIStackView可以通过本机视图获得更好的效率。

https://github.com/amitcse6/BottomAlignStackView

import UIKit
import SnapKit
class ViewController: UIViewController {
    private var storeBack: UIView?
    private var myImageView: UIImageView?
    private var myScrollView: UIScrollView?
    private var myStackView: UIStackView?
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor=UIColor.white
        self.loadStoreBack()
        self.loadBackgroundImage()
        self.loadScrollView()
        self.loadStackView()
        self.loadUI()
    }
    func loadStoreBack() {
        self.storeBack=UIView()
        self.view.addSubview(self.storeBack!)
        self.storeBack?.backgroundColor=UIColor.white
        self.storeBack?.snp.remakeConstraints { (make) in
            make.top.equalTo(self.view!.snp.top)
            make.left.equalTo(self.view.snp.left)
            make.right.equalTo(self.view.snp.right)
            make.bottom.equalTo(self.view.snp.bottom)
        }
    }
    func loadBackgroundImage() -> Void {
        self.myImageView = UIImageView()
        self.storeBack?.addSubview(self.myImageView!)
        self.myImageView?.contentMode = .scaleAspectFill
        self.myImageView?.image=UIImage(named: "welcome-background")
        self.myImageView?.snp.remakeConstraints { (make) in
            make.top.equalTo(self.storeBack!.snp.top)
            make.left.equalTo(self.storeBack!.snp.left)
            make.right.equalTo(self.storeBack!.snp.right)
            make.bottom.equalTo(self.storeBack!.snp.bottom)
        }
    }
    func loadScrollView() {
        self.myScrollView = UIScrollView()
        self.storeBack?.addSubview(self.myScrollView!)
        self.myScrollView?.backgroundColor=UIColor.clear
        self.myScrollView?.showsHorizontalScrollIndicator = false
        self.myScrollView?.showsVerticalScrollIndicator = false
        self.myScrollView?.bounces=false
        self.myScrollView?.isScrollEnabled=true
        self.myScrollView?.snp.remakeConstraints { (make) in
            make.top.equalTo(self.storeBack!.snp.top)
            make.left.equalTo(self.storeBack!.snp.left)
            make.right.equalTo(self.storeBack!.snp.right)
            make.bottom.equalTo(self.storeBack!.snp.bottom)
            make.width.equalTo(self.storeBack!)
            make.height.equalTo(self.storeBack!)
        }
    }
    func loadStackView() {
        self.myStackView = UIStackView()
        self.myScrollView?.addSubview(self.myStackView!)
        self.myStackView?.backgroundColor=UIColor.clear
        self.myStackView?.axis = .vertical
        self.myStackView?.spacing = 0
        //self.myStackView?.alignment = .bottom
        self.myStackView?.snp.remakeConstraints { (make) in
            make.top.equalTo(self.myScrollView!.snp.top)
            make.left.equalTo(self.myScrollView!.snp.left)
            make.right.equalTo(self.myScrollView!.snp.right)
            make.bottom.equalTo(self.myScrollView!.snp.bottom)
            make.width.equalTo(self.myScrollView!)
            make.height.equalTo(self.myScrollView!).priority(250)
        }
    }
    func loadUI() {
        for n in 0..<5 {
            if n%2 == 0 {
                loadBuddyLogoImageView1()
            }else{
                loadBuddyLogoImageView2()
            }
        }
    }
    func loadBuddyLogoImageView1() {
        let subBackView=UIView()
        self.myStackView?.addArrangedSubview(subBackView)
        subBackView.backgroundColor=UIColor.clear
        let backgroundView=UIView()
        subBackView.addSubview(backgroundView)
        backgroundView.backgroundColor=UIColor.red
        backgroundView.snp.remakeConstraints { (make) in
            make.top.equalTo(subBackView.snp.top)
            make.left.equalTo(subBackView.snp.left)
            make.right.equalTo(subBackView.snp.right)
            make.bottom.equalTo(subBackView.snp.bottom)
            make.height.equalTo(100)
        }
    }
    func loadBuddyLogoImageView2() {
        let subBackView=UIView()
        self.myStackView?.addArrangedSubview(subBackView)
        subBackView.backgroundColor=UIColor.clear
        let backgroundView=UIView()
        subBackView.addSubview(backgroundView)
        backgroundView.backgroundColor=UIColor.green
        backgroundView.snp.remakeConstraints { (make) in
            make.top.equalTo(subBackView.snp.top)
            make.left.equalTo(subBackView.snp.left)
            make.right.equalTo(subBackView.snp.right)
            make.bottom.equalTo(subBackView.snp.bottom)
            make.height.equalTo(100)
        }
    }
}

0 个答案:

没有答案