Search code examples
iosswiftautolayout

how to add image view with some constraints programmatically using swift?


I am new in iOS Development. If using storyboard, I can place an Image view in view controller like this

enter image description here

I need to make something like that programmatically.

so I have custom view from a library called RevealingSplashView , but I need to add an image view to that custom UI View. I just know to add the image view, maybe something like this

let imageName = "yourImage.png"
let image = UIImage(named: imageName)
let imageView = UIImageView(image: image!)
imageView.frame = CGRect(x: 0, y: 0, width: 100, height: 200)
revealingSplashView.addSubview(imageView)

but I don't know how to set that constraint to image view to

a. align to center x to superview

b. proportional width to superview 0.8

c. height constraint = 25

d. align bottom to safe area = 32

how to do that ?

here is the code I use before want to add image view

let screenSize = UIScreen.main.bounds
            let iconWidth = (screenSize.width) * 0.8
            let iconHeight = iconWidth * 1 // ratio 1:1
            revealingSplashView = RevealingSplashView(iconImage: UIImage(named: "Loading Page Asset")!,iconInitialSize: CGSize(width: iconWidth, height: iconHeight), backgroundColor: AppColor.mainYellow.getUIColor())
            revealingSplashView.animationType = SplashAnimationType.twitter
            revealingSplashView.imageView?.contentMode = .scaleAspectFill


            // add loading indicator to RevealingSplashView Programatically
            revealingSplashViewIndicator.color = UIColor.white
            revealingSplashViewIndicator.frame = CGRect(x: 0.0, y: 0.0, width: 30.0, height: 30.0)
            revealingSplashViewIndicator.center =  CGPoint(x: self.view.center.x, y: self.view.center.y + (iconHeight/2) + 64 )
            revealingSplashView.addSubview(revealingSplashViewIndicator)
            revealingSplashViewIndicator.bringSubviewToFront(self.revealingSplashView)
            revealingSplashViewIndicator.startAnimating()

            let window = UIApplication.shared.keyWindow
            window?.addSubview(revealingSplashView)

Solution

  • I recommend using anchors:

    let imageName = "yourImage.png"
    let image = UIImage(named: imageName)
    let imageView = UIImageView(image: image!)
    imageView.frame = CGRect(x: 0, y: 0, width: 100, height: 200)
    revealingSplashView.addSubview(imageView)
    
    // you need to turn off autoresizing masks (storyboards do this automatically)
    imageView.translatesAutoresizingMaskIntoConstraints = false
    
    // setup constraints, it is recommended to activate them through `NSLayoutConstraint.activate`
    // instead of `constraint.isActive = true` because of performance reasons
    NSLayoutConstraint.activate([
        imageView.centerXAnchor.constraint(equalTo: revealingSplashView.centerXAnchor),
        imageView.widthAnchor.constraint(equalTo: revealingSplashView.widthAnchor, multiplier: 0.8),
        imageView.heightAnchor.constraint(equalToConstant: 25),
        imageView.bottomAnchor.constraint(equalTo: revealingSplashView.safeAreaLayoutGuide.bottomAnchor, constant: -32),
        ])