简体   繁体   中英

Center an icon image which is inside image which is inside stackview

Hey guys i want to center an image inside an image which is inside stackview.

let stackview = UIStackView()
stackview.addArrangedSubview(imageview)
imageview.addSubview(anotherimageview)

I want anotherimageview centered which is an icon image.

After adding anotherImageView as a subview of imageView setup its constraints in the way you want.

So, after this line:

imageview.addSubview(anotherimageview)

Add the following:

anotherimageview.translatesAutoresizingMaskIntoConstraints = false
anotherimageview.centerXAnchor.constraint(equalTo: imageview.centerXAnchor).isActive = true
anotherimageview.centerYAnchor.constraint(equalTo: imageview.centerYAnchor).isActive = true
// And do the same for the width and height constraints.

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