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 |
---------------------------
Try this:
cell.autoresizingMask = UIViewAutoresizingFlexibleRightMargin |
UIViewAutoresizingFlexibleBottomMargin;