NEW: Subscribe to Hacking with Swift+ and accelerate your learning! >>

How spacing StackView`s elements?

Forums > Swift

Good night. I`m dont understand how create spacing with elements inside StackView.

class ViewController: UIViewController {

    var containerForArchorPoint:  UIStackView!

    var sliderArchorPointX:   UISlider!
    var sliderArchorPointY:   UISlider!

    var labelArchorPointX:    UILabel!
    var labelArchorPointY:    UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()

        containerForArchorPoint               = UIStackView(frame: CGRect(x: 0, y: 0, width: 360, height: 120))
        containerForArchorPoint.axis          = .vertical
        containerForArchorPoint.spacing       = 30
        containerForArchorPoint.alignment     = .fill
        containerForArchorPoint.distribution  = .fill
        containerForArchorPoint.center        = CGPoint(x: self.view.center.x, y: self.view.bounds.maxY - containerForArchorPoint.bounds.height * 2)
        view.addSubview(containerForArchorPoint)

        sliderArchorPointX                    = UISlider(frame: CGRect(x: 0, y: 0, width: self.containerForArchorPoint.bounds.width, height: 30))

        sliderArchorPointY                    = UISlider(frame: CGRect(x: 0, y: 0, width: self.containerForArchorPoint.bounds.width, height: 30))
        //sliderArchorPointY.center.y        = self.sliderArchorPointX.frame.maxY + sliderArchorPointY.frame.height

        labelArchorPointX                     = UILabel(frame: CGRect(x: 0, y: 0, width: 200, height: 30))
        labelArchorPointX.layer.borderWidth   = 1

        labelArchorPointY                     = UILabel(frame: CGRect(x: 0, y: 0, width: 200, height: 30))
        labelArchorPointY.layer.borderWidth   = 1

        [sliderArchorPointX, labelArchorPointX, sliderArchorPointY, labelArchorPointY].forEach {
            containerForArchorPoint.addSubview($0)
        }
    }
}

   

With UIStackView you should call addArrangedSubview instead of addSubview

   

@roosterboy Thank you very much helped

   

Hacking with Swift is sponsored by Paw

SPONSORED Use Paw to build, test and describe web APIs. Paw has a lightning fast native macOS interface to compose requests, collaborate in real-time on API specs, and generate client code for your applications. You can import and export API definitions.

Discover Paw for Mac

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

Not logged in

Log in
 

Link copied to your pasteboard.