autoresizingMask 和固定边距的问题

发布于 2024-10-02 06:24:31 字数 572 浏览 1 评论 0原文

我试图让它与 ViewController 视图中的代码一起使用:

alt text

但我无法将其实现工作。

我已经尝试过

-(void)loadView {
    UIView *contentView = [[[UIView alloc] init] autorelease];
    self.view = contentView;
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(5,5,0,100);
    button.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    [contentView addSubview:button];
}

,但按钮的宽度仍然是超级视图的宽度......

我确信有一个简单的解释。

谢谢大家!

安东尼奥

I'm trying to get this to work with code in a ViewController's view:

alt text

but I can't get it to work.

I've tried

-(void)loadView {
    UIView *contentView = [[[UIView alloc] init] autorelease];
    self.view = contentView;
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(5,5,0,100);
    button.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    [contentView addSubview:button];
}

but the width of the button is still the width of the superview...

I'm sure there's an easy explanation.

Thanks, everybody!

Antonio

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

心意如水 2024-10-09 06:24:31

-initWithFrame: 是 UIView 的指定初始化器。在 Mac 端(NSView),我知道使用 -init 而不是 -initWithFrame: 时可能会发生奇怪的事情。也许这就是问题所在?

-initWithFrame: is the designated initializer for UIView. On the Mac side (NSView) I know strange things can happen when using -init instead of -initWithFrame:. Perhaps this is the problem?

红颜悴 2024-10-09 06:24:31

我不用担心 autoresizingMask 标志,而是在自定义 UIView 子类中重写 layoutSubviews

- (void)loadView {
    self.view = [ContentView view];
    self.view.frame = CGRectMake(0, 0, 30, 100);
}

其中 ContentView 定义为:

@interface ContentView : UIView {
    UIButton *button;
}

+ (id)view;

@end

@implementation ContentView {

+ (id)view {
    return [[self new] autorelease];
}

- (id)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        button = [UIButton buttonWithType:UIButtonTypeCustom];
        [self addSubview:button]; // Retains the button.
    }
    return self;
}

- (void)layoutSubviews {
    const CGFloat margin = 5;

    CGRect size = self.frame.size;
    button.frame = CGRectMake(margin, margin,
        size.width - 2 * margin,
        size.height - 2 * margin);
}

}

Instead of bothering with the autoresizingMask flag, I override layoutSubviews in custom UIView subclass:

- (void)loadView {
    self.view = [ContentView view];
    self.view.frame = CGRectMake(0, 0, 30, 100);
}

where ContentView is defined as:

@interface ContentView : UIView {
    UIButton *button;
}

+ (id)view;

@end

@implementation ContentView {

+ (id)view {
    return [[self new] autorelease];
}

- (id)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        button = [UIButton buttonWithType:UIButtonTypeCustom];
        [self addSubview:button]; // Retains the button.
    }
    return self;
}

- (void)layoutSubviews {
    const CGFloat margin = 5;

    CGRect size = self.frame.size;
    button.frame = CGRectMake(margin, margin,
        size.width - 2 * margin,
        size.height - 2 * margin);
}

}

-黛色若梦 2024-10-09 06:24:31

试试这个代码。

- (void)loadView
{
    CGRect screenBounds = [UIScreen mainScreen].bounds;
    UIView *contentView = [[[UIView alloc] initWithFrame:screenBounds] autorelease];
    self.view = contentView;
    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    button.frame = CGRectMake(5,5,screenBounds.size.width - 10,100);
    [button setTitle:@"hello" forState:UIControlStateNormal];
    button.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    [contentView addSubview:button];
}

Try this code.

- (void)loadView
{
    CGRect screenBounds = [UIScreen mainScreen].bounds;
    UIView *contentView = [[[UIView alloc] initWithFrame:screenBounds] autorelease];
    self.view = contentView;
    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    button.frame = CGRectMake(5,5,screenBounds.size.width - 10,100);
    [button setTitle:@"hello" forState:UIControlStateNormal];
    button.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    [contentView addSubview:button];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文