create View and label button Programatically (swift 4)


   



** 
property of imageview and label
    let artworkView = UIImageView()

     let storyLabel = UILabel()
let firstChoiceButton = UIButton(type: .system)

    let secondChoiceButton = UIButton(type: .system)


override func viewWillLayoutSubviews() {
        super.viewWillLayoutSubviews()
        

        view.addSubview(artworkView)
        artworkView.translatesAutoresizingMaskIntoConstraints = false
        
        NSLayoutConstraint.activate([
            
            artworkView.topAnchor.constraint(equalTo: view.topAnchor),
            artworkView.bottomAnchor.constraint(equalTo: view.bottomAnchor),
            artworkView.rightAnchor.constraint(equalTo: view.rightAnchor),
            artworkView.leftAnchor.constraint(equalTo: view.leftAnchor)

            ])




       view.addSubview(storyLabel)
        storyLabel.numberOfLines = 0
        storyLabel.translatesAutoresizingMaskIntoConstraints = false
        
        
        NSLayoutConstraint.activate([
            
            storyLabel.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 16),
            storyLabel.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -16),
            storyLabel.topAnchor.constraint(equalTo: view.centerYAnchor, constant: -48)
            ])

        

   view.addSubview(firstChoiceButton)
        firstChoiceButton.translatesAutoresizingMaskIntoConstraints = false
        
        NSLayoutConstraint.activate([
            firstChoiceButton.centerXAnchor.constraint(equalTo: view.centerXAnchor),
            firstChoiceButton.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -80.0)
            ])
        
        view.addSubview(secondChoiceButton)
        secondChoiceButton.translatesAutoresizingMaskIntoConstraints = false
        
        NSLayoutConstraint.activate([
            secondChoiceButton.centerXAnchor.constraint(equalTo: view.centerXAnchor),
            secondChoiceButton.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -32)
            ])
        

        


        
    }

Previous
Next Post »