autoresizing mask如何工作?

J M*_*Max 5 cocoa-touch objective-c ios autoresizingmask

我发现了很多关于自动调整的信息,但没有一个能够解决我的问题.我有一个ViewController(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];
}
Run Code Online (Sandbox Code Playgroud)

它调用的视图(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];
}
Run Code Online (Sandbox Code Playgroud)

出于某种原因,当设备是横向时,我无法获得自动调整遮罩来调整按钮宽度.任何帮助是极大的赞赏.

Dar*_*man 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];
}
Run Code Online (Sandbox Code Playgroud)