Here I have attached the screenshot that I have worked. I am facing an issue that image view is not scaling to the frame. I have created a custom view within the custom view i have added imageview and set constraints to its parent view (here containerview)
image content mode is scaleAspectFit
let imageView = makeImageView()
addSubview(imageView)
imageView.topAnchor.constraint(equalTo: topAnchor).isActive = true
imageView.leadingAnchor.constraint(equalTo: leadingAnchor).isActive = true
imageView.trailingAnchor.constraint(equalTo: trailingAnchor).isActive = true
imageView.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true
When I initialise the custom view into the viewcontroller shows like above screenshots. In viewcontroller I am just adding the image to the stackview.
Can you please let me know what should be added?
2
Answers
You may set the following properties to scale the size of the UIImageView as per the image size –
You need to leave the image view’s content mode set to
.contentMode = .scaleToFill
.Then, when you set its
.image
, set a proportional height anchor with:Here is an example custom class – I’ve inset the image view by 8-points on all 4 sides for clarity:
and an example view controller… vertical stack view containing a "top label" the custom view and a "bottom label". Also loads 3 images – at 300×300, 300×200 and 200×300. Tapping anywhere cycles through to the next image:
Using these three images:
It looks like this when running: