Autoresizingmask无法正常工作

时间:2014-09-20 12:09:04

标签: ios objective-c iphone uiview autoresizingmask

我在屏幕上以编程方式放置了一个带有框架(0,100,50,50)的UIView,并将其autoresizingMask设置为UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin当我开始旋转屏幕时,由于某种原因,视图会粘到右边缘。如果我设置了视图框(20,100,50,50),一切都很好。

我做错了什么?

示例代码:

- (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;
}

轮换前:

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

轮换后一切都很糟糕:

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

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

如果我改变

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

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

轮换前:

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

轮换后一切正常:

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

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

1 个答案:

答案 0 :(得分:0)

试试这个:

cell.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | 
                        UIViewAutoresizingFlexibleBottomMargin;