[英]Scroll to center of UIScrollview clicked UIButton added programmatically EDITED
[英]Center UIButton programmatically
我有一個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];
第二行會覆蓋您在第一行中設置的內容。 設置幾個標志的正確方法是:
[_notMeButton setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin];
在回答你的問題之前有一件事:
[_notMeButton setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin];
[_notMeButton setAutoresizingMask:UIViewAutoresizingFlexibleRightMargin];
第二句覆蓋第一句,你應該使用|
運算符,因此應用了大小調整掩碼:
[_notMeButton setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin];
要在視圖更改其大小時將元素保持在原位,您需要告訴它不要使用任何大小調整掩碼:
[_notMeButton setAutoresizingMask:UIViewAutoresizingNone];
Interface Builder中的等價物如下:
我希望它有所幫助
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.