Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Set initial position of subview

anonymous2233anonymous2233 Posts: 6 Noob
edited October 2013 in iOS SDK Development
So i want to add subview (UIImageView) to my main view with the following code:

NSString *fileLocation = [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:@background_main.png];
_aImage = [[UIImage alloc] initWithContentsOfFile:fileLocation];
_aImageView = [[UIImageView alloc] initWithImage:_aImage];
_aImageView.frame = CGRectMake(-200, 0, _aImage.size.width, _aImage.size.height);
[self.view addSubview:_aImageView];

as you can see - i'm adding UIImageView to position (-200;0);

But it shows up at default position : (0;0).

My view hierarchy:

1. self.view -main view where i'm adding subview(UIImageView);
2. _aImageView - UIImageView that i'm adding.

Results:
NSLog(@image is %@",_aImageView); //result (-200 0; 420 568)

CGPoint test = [_aImageView convertPoint:_aImageView.frame.origin toView:self.view];
NSLog(@convert %f %f,test.x,test.y); //result  (-400.000000; 0.000000)

I know that position is still (0;0) because i can see it when i testing my app. Later when i'm changing position of _aImageView to (-50;0) it's shows black screen. I know i'm missing something very important but i can't figure out why it shows up at (0;0) position.

Links to similar questions that i have checked:

- http://stackoverflow.com/questions/3518804/how-to-addsubview-with-a-position/3518936#3518936
- http://stackoverflow.com/questions/13663230/ios-add-subview-with-a-fix-position-on-screen

Replies

  • BrianSlickBrianSlick Treadmill Desk Ninja Posts: 10,687 @ @ @ @ @ @ @ @
    The numbers from your log are correct. Are you using autolayout?

    I'd like to see the image by itself, and then with the app running when you say it is wrong.
    Professional iOS App Development. Available for hire.
    BriTer Ideas LLC - WWW | Facebook | Twitter | LinkedIn

    BTIKit | BTICoreDataKit | SlickShopper 2 | Leave a PayPal donation
  • anonymous2233anonymous2233 Posts: 6 Noob
    edited October 2013
    No, i'm not using auto layout:

    First picture - moving with the following code
    _aImageView.frame = CGRectMake(100, 0, _aImage.size.width, _aImage.size.height); // "parallax"
    

    Second picture - moving to 0;0 position -
    _aImageView.frame = CGRectMake(0, 0, _aImage.size.width, _aImage.size.height); // "parallax"
    

    it's just when i'm changing frame of the _aImageView in init method nothing happens
    Post edited by anonymous2233 on
  • BrianSlickBrianSlick Treadmill Desk Ninja Posts: 10,687 @ @ @ @ @ @ @ @
    The only part that doesn't make sense is it appears to have shifted to the right, when it should be to the left due to the negative number. The second one looks correct to me.
    Professional iOS App Development. Available for hire.
    BriTer Ideas LLC - WWW | Facebook | Twitter | LinkedIn

    BTIKit | BTICoreDataKit | SlickShopper 2 | Leave a PayPal donation
  • dev666999dev666999 Posts: 3,558 @ @ @ @ @
    Is the image the correct size... or is it sitting on a clear canvas that is larger than your actual image?

    IE... does your image have a clear border on any of the 4 sides?
  • anonymous2233anonymous2233 Posts: 6 Noob
    BrianSlick wrote: »
    The only part that doesn't make sense is it appears to have shifted to the right, when it should be to the left due to the negative number. The second one looks correct to me.
    Yeah, sorry. Fixed that

  • anonymous2233anonymous2233 Posts: 6 Noob
    dev666999 wrote: »
    Is the image the correct size... or is it sitting on a clear canvas that is larger than your actual image?

    IE... does your image have a clear border on any of the 4 sides?

    It's sitting on canvas that is larger that image.
    Yeah, clear border.

    The problem with initialisation frame, doesn't matter how i change it, it doesn't change on screen
  • Duncan CDuncan C Posts: 9,114 @ @ @ @ @ @ @
    No, i'm not using auto layout:

    First picture - moving with the following code
    _aImageView.frame = CGRectMake(100, 0, _aImage.size.width, _aImage.size.height); // "parallax"
    

    Second picture - moving to 0;0 position -
    _aImageView.frame = CGRectMake(0, 0, _aImage.size.width, _aImage.size.height); // "parallax"
    

    it's just when i'm changing frame of the _aImageView in init method nothing happens

    "Changing the frame of the image view in init method"? Is this code in the init method for your view controller? That's a no-no.

    You need to add views in viewDidLoad or later. In init, your view hierarchy isn't set up yet.
    Regards,
    Duncan C
    WareTo

    widehead.gif
    Animated GIF created with Face Dancer, available for free in the app store.

    I'm available for one-on-one help at CodeMentor
Sign In or Register to comment.