无法让UIButton autoresizingMask工作

时间:2011-10-06 10:23:22

标签: objective-c uiscrollview autoresizingmask

我有以下代码:

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

创建ScrollView,并放置9 UIButton

我的问题是:当我以前使用IB来放置UIButton时,按钮会自动自动调整并自行安排在iPad视图上,现在我得到了这个: :(

有什么想法吗?谢谢。 :d

1 个答案:

答案 0 :(得分:0)

awesomeView没有将其autoresizesSubviews属性设置为YES。我认为默认情况下这是NO。