【问题标题】:How does autoresizing mask work?自动调整大小的蒙版如何工作?
【发布时间】:2011-12-06 01:02:01
【问题描述】:

我找到了很多关于自动调整大小的信息,但没有一个能够解决我的问题。我有一个 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];
}

它调用的视图(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];
}

由于某种原因,当设备为横向时,我无法获得自动调整大小的掩码来调整按钮宽度。非常感谢任何帮助。

【问题讨论】:

  • 我相信您需要在 UIButton 本身上设置 autoresizingMask。
  • 试过了。该按钮最终被移出屏幕,这不是我所期望的,这意味着我并不真正了解它是如何工作的。也不是self.autoresizesSubviews = YES; 的重点,所以我不必这样做?
  • 您尝试将按钮 autoresizeMask 设置为什么?你试过 UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin ?
  • 此外,autoresizesSubviews 属性默认为 YES,因此无需像您一样设置它。这允许子视图的 autoresizingMask 属性起作用。如果不是,则不会调整大小。
  • 好吧,因为我把它拿出来了。是的,它使按钮居中,但不能按我想要的时间长。由于某种原因,高度和宽度值没有交换。

标签: objective-c ios cocoa-touch autoresizingmask


【解决方案1】:

如果您希望它保持相同的高度,但只是保持完全居中,左右各 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];
}

【讨论】:

    猜你喜欢
    • 2017-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-23
    • 2022-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多