Question

Je suis en train d'obtenir ce à travailler avec le code dans la vue d'un ViewController:

text alt

mais je ne peux pas le faire au travail.

J'ai essayé

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

mais la largeur du bouton est toujours la largeur de la superview ...

Je suis sûr qu'il ya une explication simple.

Merci, tout le monde!

Antonio

Était-ce utile?

La solution

-initWithFrame: est l'initialiseur désigné pour UIView. Du côté Mac (NSView) Je sais que des choses étranges peuvent se produire lors de l'utilisation -init au lieu de -initWithFrame:. Peut-être est le problème?

Autres conseils

Au lieu de prendre la peine avec le drapeau de autoresizingMask, je layoutSubviews passer outre dans la sous-classe de UIView personnalisée:

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

ContentView est défini comme suit:

@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);
}

}

Essayez ce 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];
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top