以编程方式中心UIButton
问题描述:
我有一个UIButton,我以编程方式添加。这里没有问题,因为它应该和看起来不错。 但是,当我将手机倾斜到风景时,按钮不正确,我希望它仍居中。以编程方式中心UIButton
肖像:
景观:
我已经尝试了一堆东西,但似乎无法得到它的工作,在我看来,该自动尺寸应该然而照顾它。 ..在这种情况下它不起作用。
- (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;
}
答
您对自动调整面膜的想法是对的,但你正确设置它:
[_notMeButton setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin];
[_notMeButton setAutoresizingMask:UIViewAutoresizingFlexibleRightMargin];
第二行覆盖了你在第1套。设置多个标志正确的方法是:
[_notMeButton setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin];
答
一件事之前,我回答你的问题:
[_notMeButton setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin];
[_notMeButton setAutoresizingMask:UIViewAutoresizingFlexibleRightMargin];
第二句覆盖第一个,你应该使用|
运营商这样既调整掩码适用于:
[_notMeButton setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin];
要保持元素的地方是当视图改变其大小,你需要告诉它不使用任何调整面膜:
[_notMeButton setAutoresizingMask:UIViewAutoresizingNone];
而且在Interface Builder中相当于是这样的:
我希望它能帮助