2
votes

I placed a UIView with frame (0, 100, 50, 50) programmatically on the screen and set its autoresizingMask to UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin and when I begin rotate the screen the view sticks to the right edge, for some reason. If I set view frame (20, 100, 50, 50) everything works well.

What am I doing wrong?

Sample code:

- (void)viewDidLoad
{
    [super viewDidLoad];
    UIView *cell = [[UIView alloc] initWithFrame:CGRectMake(0, 100, 50, 50)];
    cell.backgroundColor = [UIColor blackColor];
    cell.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | 
                            UIViewAutoresizingFlexibleLeftMargin;
    [self.view addSubview:cell];
}
- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskAll;
}

Before rotation:

 --------------------------- 
|                           |
|=====                      |
||   |                      |
|=====                      |
|                           |
|                           |
|                           |
|                           |
|                           |
|                           |
|                           |
|                           |
|                           |
|                           |
|                           |
-----------------------------
|             o             |
 --------------------------- 

After rotation everything works bad:

 ------------------------------------------------
|                                            |   |
|                                       =====|   |
|                                       |   ||   |
|                                       =====|   |
|                                            |   |
|                                            | o |
|                                            |   |
|                                            |   |
|                                            |   |
|                                            |   |
|                                            |   |
 ------------------------------------------------

 --------------------------- 
|                           |
|                      =====|
|                      |   ||
|                      =====|
|                           |
|                           |
|                           |
|                           |
|                           |
|                           |
|                           |
|                           |
|                           |
|                           |
-----------------------------
|             o             |
 --------------------------- 

If I change

UIView *cell = [[UIView alloc] initWithFrame:CGRectMake(0, 100, 50, 50)];

to

UIView *cell = [[UIView alloc] initWithFrame:CGRectMake(20, 100, 50, 50)];

Before rotation:

 --------------------------- 
|                           |
|   =====                   |
|   |   |                   |
|   =====                   |
|                           |
|                           |
|                           |
|                           |
|                           |
|                           |
|                           |
|                           |
|                           |
-----------------------------
|             o             |
 --------------------------- 

After rotation everything works good:

 -------------------------------------------------
|                                             |   |
|   =====                                     |   |
|   |   |                                     |   |
|   =====                                     |   |
|                                             |   |
|                                             | o |
|                                             |   |
|                                             |   |
|                                             |   |
|                                             |   |
|                                             |   |
 -------------------------------------------------

 --------------------------- 
|                           |
|   =====                   |
|   |   |                   |
|   =====                   |
|                           |
|                           |
|                           |
|                           |
|                           |
|                           |
|                           |
|                           |
|                           |
-----------------------------
|             o             |
 --------------------------- 
1
What do you want to happen with your original x=0 frame?rob mayoff
At least the view has to come back to an initial position when I turn the device back to portrait orientation. And I dont understand why view sticks to right edge?Juancho

1 Answers

0
votes

Try this:

cell.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | 
                        UIViewAutoresizingFlexibleBottomMargin;