3

Im adding a UIImageView with a UIImage which I wish to appear in the center top of the main UIView.

I will be using a UIPageControl to swipe that view off to the left and bring in the new one from the right. I have added the UIImageView which normally just adds it to the bottom right corner

-(void)createUIViews{
    UIImage *image1 = [UIImage imageNamed:@"GiftCard.png"];
    firstView = [[UIImageView alloc] initWithImage:image1];
    firstView.backgroundColor = [UIColor grayColor];
    firstView.tag = 1;
    firstView.autoresizingMask = (UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin);
}

Now I wish to position it at top center. So Im thinking of something like this:

CGRect newFrame = self.view.frame;
    newFrame.origin.x += 50;
    self.view.frame = newFrame;

    [self.view addSubview:firstView];

But when I add this code at the bottom of the previous one, (which btw is being called from viewDidLoad), the image still appears at the bottom right. How do I position it correctly?

Khaled Barazi
  • 8,681
  • 6
  • 42
  • 62
marciokoko
  • 4,988
  • 8
  • 51
  • 91

2 Answers2

3

Move your code to viewDidAppear. Bounds and frames are not guaranteed to have been set till viewWillAppear/viewDidAppear.

Khaled Barazi
  • 8,681
  • 6
  • 42
  • 62
1

Make sure you set your struts and springs appropriately and then just do this:

 -(void)viewWillAppear:(BOOL)animated 
  {
    [super viewWillAppear:animated];
    firstView.center = self.center;
  }
Jack Freeman
  • 1,414
  • 11
  • 18