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 7582611
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T18:28:38+00:00 2026-05-30T18:28:38+00:00

Creating UIImageView with some offset is quite common task when you’re building interface in

  • 0

Creating UIImageView with some offset is quite common task when you’re building interface in code.
I can see two ways to initialize UIImageView with origin not equal to (0,0):

First way requires only image filename and origin, but contains a lot of code (we can reduce number of lines by one using frame.origin = CGPointMake(x,y); ):

UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"image_filename"]];
CGRect frame = imgView.frame;
frame.origin.x = 150;
frame.origin.y = 100;
undoBg.frame = frame;

Second way has much less code, looks cleaner but we need to hardcode image size:

UIImageView *shadowView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 150, 800, 600)];
shadowView.image = [UIImage imageNamed:@"image_filename"];

What is best practice for you and why?
Thanks.

  • 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-30T18:28:39+00:00Added an answer on May 30, 2026 at 6:28 pm

    Hardcoding the images sizes is a form of Unnamed numerical constants which is an indication of Code Smell

    This sort of thing should be avoided as much as possible as it can generate code that is a lot harder to maintain and is prone to human introduced errors. For example what happens when your graphic artist changes the size of the image? Instead of changing just one thing (the image) you now have to change many things (the image, and every place in the code where the image size has been hard coded)

    Remember that you code not for today, but for the people who will come after you and maintain your code.

    If anything, if you were really concerned about the extra lines of code, then you would abstract loading the UIImageView into a category, so that it can be used everywhere (note that this code is not tested):

    @interface UIImageView (MyExtension)
    -(UIImageView*)myLoadImage:(NSString*)named at:(CGPoint)location;
    @end
    
    @implementation
    -(UIImageView*)myLoadImage:(NSString*)named at:(CGPoint)location
    {
      UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:named]];
      CGRect frame = imgView.frame;
      frame.origin.x = location.x;
      frame.origin.y = location.y;
      return imgView;
    }
    @end
    

    Then you could simply do:

    UIImageView* imageView = [UIImageView myLoadImage:@"image_filename" at:CGPointMake(150,100)]; 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How do I create a UIImageView in code, instead of creating it in my
I am creating a UIView containing some text that partially covers a UIImageView. I
I am creating a custom UITableViewCell to include an UIImageView and some related text.
Could someone give me a little direction on creating crossfades/dissolves between two UIImageViews in
Creating hashes of hashes in Ruby allows for convenient two (or more) dimensional lookups.
I am creating an app, in which I have two small imageview (using have
When i first started using monotouch i found a page with some code samples
I'm creating an app where within a UIView subclass I have several UIImageView 's
I'm using a UIImageView as the accessoryView in a UITableViewCell that I'm creating programmatically.
I am creating a NC Widget and can't seem to adjust the height of

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.