简体   繁体   中英

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

在此处输入图片说明

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)

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),
    ])
revealingSplashView.addSubview(imageView)

imageView.centerXAnchor.constraint(equalTo: revealingSplashView.centerXAnchor).isActive = true
imageView.widthAnchor.constraint(equalTo: revealingSplashView.widthAnchor, multiplier: 0.8).isActive = true
imageView.heightAnchor.constraint(equalToConstant: 25).isActive = true
imageView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor).isActive = true
imageView.translatesAutoresizingMaskIntoConstraints = false

To add constraints programmatically you need to set

imageView.translatesAutoresizingMaskIntoConstraints = false

then you can set the constraints:

imageView.widthAnchor.constraint(equalTo: revealingSplashView.widthAnchor, multiplier: 0.8).isActive = true
imageView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor).isActive = true
imageView.heightAnchor.constraint(equalToConstant: 25).isActive = true
imageView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true

if you want to activate constraints you need to set

isActive = true

also if you support iOS < 11 you need to put a control because you don't have the safeArea

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM