Cannot get UIButton autoresizingMask to work

2019-09-09 17:09发布

问题:

I have the following code:

- (void)loadView {
[super loadView];
UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 87, self.view.frame.size.width, self.view.frame.size.height)];
scroll.pagingEnabled = YES;
NSInteger numberOfViews = 2;
scroll.contentSize = CGSizeMake(self.view.frame.size.width * numberOfViews, self.view.frame.size.height);
scroll.autoresizingMask=(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
[self.view addSubview:scroll];
for (int i = 0; i < numberOfViews; i++) {
    CGFloat yOrigin = i * self.view.frame.size.width;
    UIView *awesomeView = [[UIView alloc] initWithFrame:CGRectMake(yOrigin, 0, self.view.frame.size.width, self.view.frame.size.height)];
    [scroll addSubview:awesomeView];
    UIButton *myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    int x=1;
    for (int i=0; i<=2; i++) {
        for (int z=0; z<=2; z++) {
            myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
            myButton.frame = CGRectMake(4+(80*z), 122+(61*i), 72, 53);
            NSString *numero=[NSString stringWithFormat:@"%d", x];
            x++;
            [myButton setTitle:numero forState:UIControlStateNormal];
            myButton.titleLabel.font = [UIFont fontWithName:@"System Bold" size:31];
            myButton.titleLabel.font = [UIFont boldSystemFontOfSize:31];
            [myButton addTarget:self action:@selector(numeros:) forControlEvents:UIControlEventTouchUpInside];
            myButton.autoresizingMask=(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
            [awesomeView addSubview:myButton];
        }
    }
}
}

Which creates an ScrollView, and put 9 UIButton's.

My problem is: when I used to use IB to put the UIButton, the buttons automatically autosized and arrange itselves on the iPad view, and now Im getting this:

Any ideas? Thanks. :D

回答1:

awesomeView does not have its autoresizesSubviews property set to YES. I think it's NO by default.