Home > other >  Swift: Autolayout programmatically bottom anchor is not working correctly
Swift: Autolayout programmatically bottom anchor is not working correctly

Time:12-04

I have set constraints for my button in the code and this is what my code looks like:

       addSubview(stackViewOne)
       
       stackViewOne.bottomAnchor.constraint(equalTo: bottomAnchor, constant: -250).isActive = true
       stackViewOne.leftAnchor.constraint(equalTo: leftAnchor, constant: 20).isActive = true
       stackViewOne.rightAnchor.constraint(equalTo: rightAnchor, constant: -20).isActive = true
       stackViewOne.heightAnchor.constraint(equalToConstant: frame.size.height * 0.065).isActive = true

but now i have problems with my bottom anchor. I want to make my stackview 80 from the bottom away. Only when I enter eighty as the distance my stackview is not displayed. Only when I enter a really high value in the minus range is the stackview only displayed. (equalTo: bottomAnchor, constant: -250)

Does anyone know why? Best regards

CodePudding user response:

You usually don't need to specify a height constraint on a stackview - the stackview itself will be sized by its arrangedsubviews (labels?). I'd start by removing that constraint. Make sure your arranged subviews have sizes and your stackview's spacing/alignment etc is configured correctly.

Also possible safeArea / layoutMargins are interfering with your layout - but impossible to know without more of your code.

CodePudding user response:

I created a UIView class and there I created objects look: import UIKit

class TimerSequenzeView: UIView {
    
    lazy var plusButton: UIButton = {
        let btn = UIButton(type: .system)
        btn.setTitle("5", for: .normal)
        btn.titleLabel?.font = .systemFont(ofSize: 35)
        btn.titleLabel?.font = UIFont(name: "Arial", size: 35)
        btn.setTitleColor(.red, for: .normal)
        btn.layer.borderColor = UIColor.red.cgColor
        btn.layer.borderWidth = 3
        btn.layer.cornerRadius = 30
        btn.setImage(UIImage(systemName: "plus"), for: .normal)
        btn.tintColor = .red
        btn.translatesAutoresizingMaskIntoConstraints = false
        
        return btn
    }()
    
    lazy var minusButton: UIButton = {
        let btn = UIButton(type: .system)
        btn.setTitle("5", for: .normal)
        btn.titleLabel?.font = .systemFont(ofSize: 35)
        btn.titleLabel?.font = UIFont(name: "Arial", size: 35)
        btn.setTitleColor(.red, for: .normal)
        btn.layer.borderColor = UIColor.red.cgColor
        btn.layer.borderWidth = 3
        btn.layer.cornerRadius = 30
        btn.setImage(UIImage(systemName: "minus"), for: .normal)
        btn.tintColor = .red
        btn.translatesAutoresizingMaskIntoConstraints = false
        
        return btn
    }()
    
    lazy var stackViewOne: UIStackView = {
        let stackView = UIStackView()
        stackView.axis = .horizontal
        stackView.distribution = .fillEqually
        stackView.spacing = 20
        stackView.translatesAutoresizingMaskIntoConstraints = false
        
        stackView.addArrangedSubview(plusButton)
        stackView.addArrangedSubview(minusButton)
        
        return stackView
    }()
    

    override init(frame: CGRect) {
        super.init(frame: frame)
        setConstrains()
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }
    
    
    func setConstrains() {
        addSubview(stackViewOne)

        stackViewOne.bottomAnchor.constraint(equalTo: safeAreaLayoutGuide.bottomAnchor, constant: -80).isActive = true
        stackViewOne.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 20).isActive = true
        stackViewOne.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -20).isActive = true
        stackViewOne.heightAnchor.constraint(equalToConstant: frame.size.height * 0.065).isActive = true
    }
}
  • Related