2012-01-18 3 views
3

У меня есть UIButton, который я добавляю программно. Здесь нет проблем, он заходит так, как надо, и выглядит хорошо. Однако, когда я наклоняю телефон к пейзажу, кнопка становится неуместной, и я хочу, чтобы она все еще была сосредоточена.Центр UIButton программно

Портрет: It's centered here

Пейзаж: Not centered here

Я попытался куча вещей, но не могу показаться, чтобы заставить его работать, то мне кажется, что автоматическое изменение должны заботиться о нем, однако. .. в этом случае он не работает.

- (UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section 
{ 
    UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 49)]; 

    _notMeButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
    UIImage *butImage = [[UIImage imageNamed:@"notme.png"] stretchableImageWithLeftCapWidth:6 topCapHeight:6]; 
    UIImage *butImagePressed = [[UIImage imageNamed:@"KC_notmePressed.png"] stretchableImageWithLeftCapWidth:6 topCapHeight:6]; 
    [_notMeButton setBackgroundImage:butImage forState:UIControlStateNormal]; 
    [_notMeButton setBackgroundImage:butImagePressed forState:UIControlStateHighlighted]; 
    [_notMeButton setTitle:NSLocalizedString(@"Change address", @"KlarnaCheckout") forState:UIControlStateNormal]; 
    [_notMeButton setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal]; 
    [_notMeButton setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted]; 
    [_notMeButton setTitleShadowColor:[UIColor whiteColor] forState:UIControlStateNormal]; 
    [_notMeButton.titleLabel setShadowColor:[UIColor whiteColor]]; 
    [_notMeButton.titleLabel setShadowOffset:CGSizeMake(0, 1)]; 
    [_notMeButton.titleLabel setFont:[UIFont systemFontOfSize:14]]; 
    [_notMeButton addTarget:self action:@selector(thisIsNotMe) forControlEvents:UIControlEventTouchUpInside]; 
    [_notMeButton setTag:1]; 
    [_notMeButton sizeToFit]; 
    [_notMeButton setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin]; 
    [_notMeButton setAutoresizingMask:UIViewAutoresizingFlexibleRightMargin]; 
    [_notMeButton setFrame:CGRectMake(10, 10, _notMeButton.frame.size.width+20, _notMeButton.frame.size.height)]; 
    [_notMeButton setCenter:footerView.center]; 

    [footerView addSubview:_notMeButton]; 

    return footerView; 
} 

ответ

6

Ваша идея о маске автоматического изменения прав, но вы устанавливаете его неправильно:

[_notMeButton setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin]; 
[_notMeButton setAutoresizingMask:UIViewAutoresizingFlexibleRightMargin]; 

Вторая строка переписывает то, что вы установили в 1-ом. Правильный способ установить несколько флагов будет:

[_notMeButton setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin]; 
2

Одна вещь, прежде чем я отвечу на ваш вопрос:

[_notMeButton setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin]; 
[_notMeButton setAutoresizingMask:UIViewAutoresizingFlexibleRightMargin]; 

Второе предложение перезаписывает первый, вы должны использовать оператор | так как изменение размера маски являются применяется:

[_notMeButton setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin]; 

чтобы сохранить элемент, где, когда представление изменяет свой размер, вы должны сказать ему, чтобы не использовать какие-либо изменения размера маски:

[_notMeButton setAutoresizingMask:UIViewAutoresizingNone]; 

И эквивалент в Interface Builder это:

IB autoresizing mask applier

Я надеюсь, что это помогает

Смежные вопросы