2

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

替代文字

但我无法让它工作。

我试过了

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

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

我敢肯定有一个简单的解释。

谢谢大家!

安东尼奥

4

3 回答 3

0

-initWithFrame:是 UIView 的指定初始化器。在 Mac 端(NSView)我知道使用-init而不是-initWithFrame:. 也许这就是问题所在?

于 2010-11-15T15:57:53.047 回答
0

我没有打扰autoresizingMask标志,而是layoutSubviews在自定义UIView子类中覆盖:

-(无效)加载视图{
    self.view = [内容视图视图];
    self.view.frame = CGRectMake(0, 0, 30, 100);
}

其中ContentView定义为:

@interface 内容视图:UIView {
    UIButton *按钮;
}

+(id)视图;

@结尾

@实现内容视图 {

+ (id)view {
    返回[[自我新]自动释放];
}

- (id)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        按钮 = [UIButton buttonWithType:UIButtonTypeCustom];
        [自我添加子视图:按钮];// 保留按钮。
    }
    回归自我;
}

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

    CGRect 大小 = self.frame.size;
    button.frame = CGRectMake(边距,边距,
        size.width - 2 * 边距,
        size.height - 2 * 边距);
}

}

于 2010-12-09T11:27:53.867 回答
0

试试这个代码。

- (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];
}
于 2011-07-14T00:56:25.740 回答