2011-12-05 32 views
5

我發現了很多關於autoresizing的信息,但沒有一個能夠解決我的問題。我有一個視圖控制器(RAViewController)誰在它的loadView方法調用視圖像這樣:自動調整掩碼如何工作?

- (void)loadView 
{ 
    // Set Navigation Bar Title 
    self.navigationItem.title = @"RA"; 

    // Add Background view 
    self.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 

    // Add RAView 
    RAView *rAView = [[RAView alloc] initWithFrame:self.view.frame Controller:self]; 
    [self.view rAView]; 
} 

它調用視圖(RAView)看起來是這樣的:

- (id)initWithFrame:(CGRect)frame Controller:(RAViewController *)Controller 
{ 
    self = [super initWithFrame:frame]; 
    if (self) { 
     // Initialization code 
     self.autoresizesSubviews = YES; 
     self.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; 
     self.backgroundColor = [[UIColor alloc] initWithRed:1.0 green:1.0 blue:1.0 alpha:1.0]; 

     controller = Controller; 

     // Draw Architecture View 
     [self drawArchitectureView]; 
    } 
return self; 
} 

-(void)drawArchitectureView { 
    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(10, 10, self.frame.size.width - 20, 50); 

    [button setBackgroundColor:[UIColor grayColor]]; 
    [button setTitle:@"Overview" forState:UIControlStateNormal]; 

    [self addSubview:button]; 
} 

出於某種原因,我不能當設備處於橫向模式時,獲取自動調整遮罩以調整按鈕寬度。任何幫助是極大的讚賞。

+1

我相信你會需要設置autoresizingMask上的UIButton本身。 –

+0

試過了。該按鈕最終被移出屏幕,這不是我所期望的,這意味着我不太瞭解它是如何工作的。也不是'self.autoresizesSubviews = YES;'的要點,所以我不必那樣做? –

+0

您嘗試將按鈕autoresizeMask設置爲什麼?你有沒有嘗試過UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin? –

回答

3

如果你想讓它保持相同的高度,但只是停留完全居中時,由左,右10.0,嘗試:

-(void)drawArchitectureView { 
    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(10, 10, self.frame.size.width - 20, 50); 

    button.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleRightMargin; 

    [button setBackgroundColor:[UIColor grayColor]]; 
    [button setTitle:@"Overview" forState:UIControlStateNormal]; 

    [self addSubview:button]; 
}