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の指定された初期化子です。-initMac側(NSView)では、の代わりにを使用すると奇妙なことが起こる可能性があることを私は知ってい-initWithFrame:ます。おそらくこれが問題ですか?

于 2010-11-15T15:57:53.047 に答える
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 に答える
0

フラグを気にする代わりに、カスタムサブクラスautoresizingMaskでオーバーライドします。layoutSubviewsUIView

- (無効)loadView {
    self.view = [ContentView ビュー];
    self.view.frame = CGRectMake(0, 0, 30, 100);
}

は次のようにContentView定義されます。

@interface ContentView : UIView {
    UIButton *ボタン;
}

+ (id)ビュー;

@終わり

@実装ContentView {

+ (id) ビュー {
    return [[self new] autorelease];
}

- (id)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        ボタン = [UIButton buttonWithType:UIButtonTypeCustom];
        [self addSubview:button]; // ボタンを保持します。
    }
    自分自身を返します。
}

- (void)layoutSubviews {
    const CGFloat マージン = 5;

    CGRect サイズ = self.frame.size;
    button.frame = CGRectMake(マージン、マージン、
        size.width - 2 * マージン、
        size.height - 2 * マージン);
}

}

于 2010-12-09T11:27:53.867 に答える