Проблемы с автосазовой масками и фиксированными полями

StackOverflow https://stackoverflow.com/questions/4163948

Вопрос

Я пытаюсь заставить это работать с кодом в представлении 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];
}

Но ширина кнопки все еще ширина руководства ...

Я уверен, что есть простое объяснение.

Спасибо всем!

Антонио

Это было полезно?

Решение

-initWithFrame: Является ли обозначенный инициализатор для UIView. На стороне Mac (NSView) я знаю, что может произойти странные вещи при использовании -init вместо -initWithFrame:. Отказ Возможно, это проблема?

Другие советы

Вместо того, чтобы беспокоить с autoresizingMask Флаг, я переопределяю layoutSubviews в таможении UIView Подкласс:

- (пустота) LoadView {Self.View = [ViewView View]; self.view.frame = cgrectmake (0, 0, 30, 100); }

куда ContentView определяется как:

@interface ContentView: UIView {Uibutton * кнопка; } + (ID) вид; @end @implementation ContentView {+ (ID) View {Return [[Self New] Autorelease]; } - (ID) INITWITHFRAME: (CGRECT) Рамка {Если (Self = [Super initwithframe: кадр]) {кнопка = [ubutton buttonwithtype: ubuttontypecustom]; [Self AddSubView: кнопка]; // Сохраняет кнопку. } вернуть себя; } - (void) layoutsubviews {const cgfloat margin = 5; Размер CGrect = Self.frame.size; button.frame = Cgrectmake (Margin, Margin, Size.width - 2 * Margin, Size.height - 2 * Margin); }}

Попробуйте этот код.

- (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];
}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top