【问题标题】:Cannot get UIButton autoresizingMask to work无法让 UIButton autoresizingMask 工作
【发布时间】:2011-10-06 18:30:52
【问题描述】:

我有以下代码:

- (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

【问题讨论】:

    标签: objective-c uiscrollview autoresizingmask


    【解决方案1】:

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

    【讨论】:

    • 我试着输入它:[awesomeView autoresizesSubviews:YES];,但我收到了这个错误:receiver type UIView for instance message does not declare a method with selector 'autoresizesSubviews:'
    • 使用 awesomeView.autoresizesSubviews = YES 或 [awesomeView setAutoresizesSubviews:YES]
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-29
    • 1970-01-01
    • 2020-09-25
    • 1970-01-01
    • 2020-02-23
    相关资源
    最近更新 更多