Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7638347
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T08:09:16+00:00 2026-05-31T08:09:16+00:00

I am programmatically setting up an imageview with a border. I set the content

  • 0

I am programmatically setting up an imageview with a border. I set the content mode of the imageView to Aspect Fit, which works, but the border remains the original square.

Code:

CGRect imageViewRect = CGRectMake(left, top, 158, 119); 
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:imageViewRect];
        [imageView setContentMode:UIViewContentModeScaleAspectFit];
        [imageView setImage:image];
        [imageView.layer setBorderColor:[[UIColor blackColor] CGColor]];
        [imageView.layer setBorderWidth:3.0];

Obviously, I want the black border to adjust with the aspect fit and surround the image on all sides. Instead it remains in the original frame, and looks like this:

wHAT IT LOOKS LIKE

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T08:09:18+00:00Added an answer on May 31, 2026 at 8:09 am

    Using a method slightly modified from Frank Schmitt in this post, you can add a method to work out the frame needed to display the image scaled to aspect:

    - (CGRect)getFrameSizeForImage:(UIImage *)image inImageView:(UIImageView *)imageView {
    
        float hfactor = image.size.width / imageView.frame.size.width;
        float vfactor = image.size.height / imageView.frame.size.height;
    
        float factor = fmax(hfactor, vfactor);
    
        // Divide the size by the greater of the vertical or horizontal shrinkage factor
        float newWidth = image.size.width / factor;
        float newHeight = image.size.height / factor;
    
        // Then figure out if you need to offset it to center vertically or horizontally
        float leftOffset = (imageView.frame.size.width - newWidth) / 2;
        float topOffset = (imageView.frame.size.height - newHeight) / 2;
    
        return CGRectMake(leftOffset, topOffset, newWidth, newHeight);
    }
    

    You can then call this after setting your UIImageView image:

    CGRect frame = [self getFrameSizeForImage:imageView.image inImageView:imageView]; 
    

    Finally, set the UIImageView’s frame using this frame, offsetting position for any change in width/height:

    CGRect imageViewFrame = CGRectMake(imageView.frame.origin.x + frame.origin.x, imageView.frame.origin.y + frame.origin.y, frame.size.width, frame.size.height);
    imageView.frame = imageViewFrame;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to programmatically set a cell in editing mode. I know that setting
So I'm setting a button's background doing this: b.setBackgroundResource(R.drawable.custom_button1); How do I programmatically set
i'm populating a datagrid programmatically but before setting the itemsource, i'm also programmatically adding
In ASP.NET (not MVC), what is the best approach to programmatically setting styles on
I have an ImageView with a source image set in the xml using the
I'm having trouble figuring out how to programmatically setting the Stroke of my arrow.
When setting a SearchBarDisplay, using IB or programmatically it gets a Cancel button right
I'm trying to set a shadow for my button, but instead of getting shadow
Is there a way of setting (programmatically or via interface builder) the minimum size
I'm setting up an NSTextView programmatically: NSTextView *infoTextView = [[NSTextView alloc] initWithFrame:insetRect]; [infoTextView setAutoresizingMask:NSViewWidthSizable|NSViewHeightSizable];

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.