-2

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.

swift dev
  • 5
  • 3

1 Answers1

0

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.
Mo Abdul-Hameed
  • 6,030
  • 2
  • 23
  • 36