1

I've created programmatically a View, an UIImageView and a Button. The problem is that i need to update their position if the device switches from Portrait to Landscape, how can I do it?

This should be the code that checks if the Device is in Portrait or Landscape mode

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
    if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || toInterfaceOrientation == UIInterfaceOrientationLandscapeRight)
    {


    }
    else
    {


    }
}
LS_
  • 6,763
  • 9
  • 52
  • 88

4 Answers4

1
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || toInterfaceOrientation == UIInterfaceOrientationLandscapeRight)
    {
//change the frame for sideways direction
btn.frame=CGRectMake(x,y,width,height);
imgView.frame=CGRectMake(x,y,width,height);
view.frame=CGRectMake(x,y,width,height);
    }
else
    {
//change the frame for up/down
btn.frame=CGRectMake(x,y,width,height);
imgView.frame=CGRectMake(x,y,width,height);
view.frame=CGRectMake(x,y,width,height);
    }
 }
Adnan Munir
  • 129
  • 7
0

You should use the code along the lines of [self setFrame:CGRectMake(x, y, width, height)]; In this case self will be the name of your UIImageView, View or Button

urnotsam
  • 770
  • 7
  • 24
0

Usually you should use AutoLayout for this case. You can read about it here: http://www.raywenderlich.com/50317/beginning-auto-layout-tutorial-in-ios-7-part-1

If you want something special, you can use

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration

or

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
AlKozin
  • 904
  • 8
  • 25
  • I know I should use autolayout but the I had to create 3 elements programmatically and I couldn't create them in the Interface Builder, anyways thanks :) – LS_ Jul 17 '14 at 07:02
  • @user3789527 You can add constraits in code :) NSLayoutConstraint *myConstraint =[NSLayoutConstraint constraintWithItem:mybutton attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:mylabel attribute:NSLayoutAttributeWidth multiplier:5 constant:0]; [superview addConstraint: myConstraint]; – AlKozin Jul 18 '14 at 04:36
  • damn thought I couldn't add constraints programmatically! Thanks a lot, this will save me lot of coding :P – LS_ Jul 18 '14 at 08:58
0

you do it in viewWillLayoutSubviews. Normally, you don't need to be of any orientation if all you need to do is to have frame change with respect to bounds of superview.

Boris Suvorov
  • 381
  • 2
  • 6