1

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

I want to start addArrangedSubview from bottom alignment as like as attached 2nd screenshot(3rd is my actual working screenshot). But every time it arranged from top to bottom. But I need it from bottom to top arrange. I want to create this design using UIStackView inside UIScrollView. I'm trying it with UIScrollView because of landscapes orientation support. And UIStackView for better efficiency with native view.

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)
        }
    }
}
AMIT
  • 906
  • 1
  • 8
  • 20
  • Have you tried using `func insertArrangedSubview(_ view: UIView, at stackIndex: Int)` ? – Hima Aug 08 '19 at 14:29
  • yes, I already try it. My Subview height don't large more than parents view although I use UIScrollView – AMIT Aug 09 '19 at 00:56
  • How does your code arrange it right now? Can you show the actual behavior and the desired behavior together? As I can see you only showed the desired behavior.. – denis_lor Aug 09 '19 at 09:34
  • It's my repo https://github.com/amitcse6/BottomAlignStackView – AMIT Aug 15 '19 at 15:22
  • It's still unclear what the screen shots are. What is the _first_ screen shot supposed to show? And if the second screen shot is actually the desired result, what are all those red and green rectangles for? Is that really what you are trying to achieve? It looks nothing like the third screen shot; are there really red and green rectangles in your interface? – matt Aug 15 '19 at 15:43

0 Answers0