this is my code
m_containerbackground = [UIView new];
m_containerbackground.backgroundColor = [AppleColor clearColor];
[m_containerbackground setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.view addSubview:m_containerbackground];
[m_containerbackground.centerXAnchor constraintEqualToAnchor:self.view.centerXAnchor].active = YES;
[m_containerbackground.centerYAnchor constraintEqualToAnchor:self.view.centerYAnchor].active = YES;
[m_containerbackground.widthAnchor constraintEqualToConstant:width/pointScale].active = YES;
[m_containerbackground.heightAnchor constraintEqualToConstant:height/pointScale].active = YES;
m_container = [UIView new];
[m_container setTranslatesAutoresizingMaskIntoConstraints:NO];
[m_containerbackground addSubview:m_container];
[m_container.centerXAnchor constraintEqualToAnchor:self.view.centerXAnchor].active = YES;
[m_container.centerYAnchor constraintEqualToAnchor:self.view.centerYAnchor].active = YES;
[m_container.widthAnchor constraintEqualToAnchor:m_containerbackground.widthAnchor].active = YES;
[m_container.heightAnchor constraintEqualToAnchor:m_containerbackground.heightAnchor].active = YES;
m_image = [AppleImageView new];
[m_image setTranslatesAutoresizingMaskIntoConstraints:NO];
m_image.image = m_srcImage;
m_image.contentMode = UIViewContentModeScaleAspectFit;
[m_container addSubview:m_image];
[m_image.topAnchor constraintEqualToAnchor:m_container.topAnchor constant:topm].active = YES;
[m_image.leadingAnchor constraintEqualToAnchor:m_container.leadingAnchor constant:leftm].active = YES;
[m_image.trailingAnchor constraintEqualToAnchor:m_container.trailingAnchor constant:-rightm].active = YES;
[m_image sizeToFit];
As shown in the code above, I am able to control the width of the m_image and I set the UIViewContentModeScaleAspectFit
, this is because I want the width of the m_srcImage
to scale to the same width as the m_image
and the height of the m_srcImage
to be scaled to the original aspect ratio . Then I want the height of the m_image
to be the same as the height of the scaled m_srcImage
, how do I do this?
Below is my Android code, this works under Android because Android has WRAP_CONTENT and the IOS heightAnchor constraintEqualToConstant
can only set specific values...
m_image = new ImageView(m_activity);
m_image.setId(m_image.hashCode());
m_image.setImageBitmap(m_srcImage);
m_image.setScaleType(ImageView.ScaleType.FIT_START);
m_image.setAdjustViewBounds(true);
m_container.addView(m_image);
{
ConstraintSet set = new ConstraintSet();
set.clone(m_container);
set.constrainHeight(m_image.getId(), ConstraintSet.WRAP_CONTENT);
set.constrainWidth(m_image.getId(), ConstraintSet.MATCH_CONSTRAINT);
set.connect(m_image.getId(), ConstraintSet.TOP, ConstraintSet.PARENT_ID, ConstraintSet.TOP,topm);
set.connect(m_image.getId(), ConstraintSet.RIGHT, ConstraintSet.PARENT_ID, ConstraintSet.RIGHT,rightm);
set.connect(m_image.getId(), ConstraintSet.LEFT, ConstraintSet.PARENT_ID, ConstraintSet.LEFT,leftm);
set.applyTo(m_container);
}