Programmatically applied constraints are not working in ViewController

As I’m integrating a native iOS screen in a Flutter plugin I have to set constraints programmatically for the ui as there is no storyboard.

I have these UI components:

    var overlayMaskView: UIView = UIView()
    var previewView: UIView = UIView()
    var stillImagePreviewView: UIImageView = UIImageView()
    var viewFinderShapeLabel: UILabel = UILabel()
    var viewfinderShapeType: UISegmentedControl = UISegmentedControl()
    var vehicleTypeLabel: UILabel  = UILabel()
    var vehicleType: UISegmentedControl = UISegmentedControl()

I’m reading their constraints from the storyboard but I’m struggling to replicate them programmatically.
I was able to set constraints for overlayMaskView, previewView and stillImagePreviewView four anchors to the safe area anchors.

//        view.translatesAutoresizingMaskIntoConstraints = false
        
        previewView.translatesAutoresizingMaskIntoConstraints = false
        previewView.backgroundColor = UIColor.black//.black
        previewView.contentMode = .scaleToFill
        previewView.isUserInteractionEnabled = true
        previewView.isOpaque = true
        previewView.clearsContextBeforeDrawing = true
        previewView.clipsToBounds = false
        previewView.autoresizesSubviews = true
        view.addSubview(previewView)
        previewView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor, constant: 0).isActive = true
        previewView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor, constant: 0).isActive = true
        previewView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 0).isActive = true
        previewView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor, constant: 0).isActive = true


        stillImagePreviewView.translatesAutoresizingMaskIntoConstraints = false
        stillImagePreviewView.contentMode = .scaleAspectFit
        stillImagePreviewView.isUserInteractionEnabled = false
        stillImagePreviewView.isOpaque = true
        stillImagePreviewView.clearsContextBeforeDrawing = true
        stillImagePreviewView.clipsToBounds = true
        stillImagePreviewView.autoresizesSubviews = true
        view.addSubview(stillImagePreviewView)
        stillImagePreviewView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor, constant: 0).isActive = true
        stillImagePreviewView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor, constant: 0).isActive = true
        stillImagePreviewView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 0).isActive = true
        stillImagePreviewView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor, constant: 0).isActive = true

        overlayMaskView.translatesAutoresizingMaskIntoConstraints = false
        overlayMaskView.contentMode = .scaleToFill
        overlayMaskView.isUserInteractionEnabled = true
        overlayMaskView.isOpaque = true
        overlayMaskView.clearsContextBeforeDrawing = true
        overlayMaskView.clipsToBounds = false
        overlayMaskView.autoresizesSubviews = true
        view.addSubview(overlayMaskView)
        overlayMaskView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor, constant: 0).isActive = true
        overlayMaskView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor, constant: 0).isActive = true
        overlayMaskView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 0).isActive = true
        overlayMaskView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor, constant: 0).isActive = true

Then the first label viewFinderShapeLabel which has center x aligned to the view x axis, a top anchor to the safe area top anchor with a constant of 8, leading and trailing anchors to the safe area leading and trailing anchors with a constant of 16.
So i set them all like so and it’s at desired position :

        viewFinderShapeLabel.translatesAutoresizingMaskIntoConstraints = false
        viewFinderShapeLabel.text = "Formato targa"
        viewFinderShapeLabel.textAlignment = .center
        viewFinderShapeLabel.textColor = UIColor.white
        viewFinderShapeLabel.isEnabled = true
        viewFinderShapeLabel.isUserInteractionEnabled = false
        viewFinderShapeLabel.isOpaque = false
        viewFinderShapeLabel.clearsContextBeforeDrawing = true
        viewFinderShapeLabel.autoresizesSubviews = true
        view.addSubview(viewFinderShapeLabel)
        viewFinderShapeLabel.centerXAnchor.constraint(equalTo: view.centerXAnchor, constant: 0).isActive = true
        viewFinderShapeLabel.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 8).isActive = true
        viewFinderShapeLabel.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor, constant: 16).isActive = true
        viewFinderShapeLabel.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor, constant: -16).isActive = true

Then there is the first segmented controller which also has center x aligned to the view x axis, leading and trailing anchors to the safe area leading and trailing anchors with a constant of 16, but its top anchor to the viewFinderShapeLabel’s bottom anchor with a constant of 8.

        viewfinderShapeType.translatesAutoresizingMaskIntoConstraints = false
        viewfinderShapeType = UISegmentedControl(items: ["RETTANGOLARE", "QUADRATA"])
        viewfinderShapeType.setEnabled(true, forSegmentAt: 0)
        viewfinderShapeType.setEnabled(true, forSegmentAt: 1)
        viewfinderShapeType.selectedSegmentIndex = 0
        viewfinderShapeType.isEnabled = true
        viewfinderShapeType.isUserInteractionEnabled = true
        viewfinderShapeType.clearsContextBeforeDrawing = true
        viewfinderShapeType.autoresizesSubviews = true
        view.addSubview(viewfinderShapeType)
        viewfinderShapeType.centerXAnchor.constraint(equalTo: view.centerXAnchor, constant: 0).isActive = true
        viewfinderShapeType.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor, constant: 16).isActive = true
        viewfinderShapeType.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor, constant: -16).isActive = true
        viewfinderShapeType.topAnchor.constraint(equalTo: viewFinderShapeLabel.bottomAnchor, constant: 8).isActive = true

This setup fails as the segmented control is placed at the top left corner without any spacing.

Can you spot what I’m doing wrong causing these latest constraints not to work?

Many thanks

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật