Cannot get UIButton autoresizingMask to work

梦想的初衷 提交于 2019-12-25 04:03:12

问题


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.



来源:https://stackoverflow.com/questions/7673008/cannot-get-uibutton-autoresizingmask-to-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!