![](/img/trans.png)
[英]Swift UIButton .contentVerticalAlignment property doesn't work good
[英]UIButton contentVerticalAlignment doesn't center button image
我以編程方式創建了一個UIButton並設置了它的圖像,但是該圖像不會垂直居中,而是始終在按鈕的底部進行渲染。
這是我的代碼:
CGFloat logoutButtonSize = self.profileItem.detailView.frame.size.height;
CGFloat logoutButtonX = self.profileItem.detailView.frame.size.width - logoutButtonSize - 8;
CGRect logoutButtonFrame = CGRectMake(logoutButtonX, 0, logoutButtonSize, logoutButtonSize);
UIButton *logoutButton = [[UIButton alloc] initWithFrame:logoutButtonFrame];
[logoutButton setImage:[UIImage imageNamed:@"logout.png"] forState:UIControlStateNormal];
logoutButton.imageView.contentMode = UIViewContentModeCenter;
logoutButton.contentMode = UIViewContentModeCenter;
[logoutButton addTarget:self
action:@selector(confirmLogout)
forControlEvents:UIControlEventTouchUpInside];
logoutButton.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleHeight;
logoutButton.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
logoutButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
在為按鈕的imageView設置了鮮明的背景色之后,我可以看到它仍然與底部對齊:
(藍色=按鈕,紅色=圖片視圖)
為什么不起作用?
根據您發布的圖像,看起來按鈕本身在底部被裁剪,而不是圖像未垂直居中。
您的代碼:
CGRect logoutButtonFrame = CGRectMake(logoutButtonX, 0, logoutButtonSize, logoutButtonSize);
表示按鈕(藍色矩形)應為正方形。 但是,在您發布的圖像中,藍色矩形是120 x 88
。
您的按鈕的底部未顯示,因此看起來圖像未居中。
順便說一句,您不需要太多正在使用的代碼:
// define the frame size
CGFloat logoutButtonSize = self.profileItem.detailView.frame.size.height;
CGFloat logoutButtonX = self.profileItem.detailView.frame.size.width - logoutButtonSize - 8;
CGRect logoutButtonFrame = CGRectMake(logoutButtonX, 0, logoutButtonSize, logoutButtonSize);
// instantiate the UIButton
UIButton *logoutButton = [[UIButton alloc] initWithFrame:logoutButtonFrame];
// set the image
[logoutButton setImage:[UIImage imageNamed:@"logout.png"] forState:UIControlStateNormal];
// add the button action target
[logoutButton addTarget:self
action:@selector(confirmLogout)
forControlEvents:UIControlEventTouchUpInside];
// set the resizing mask properties
logoutButton.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleHeight;
// shouldn't need any of the following
//logoutButton.imageView.contentMode = UIViewContentModeCenter;
//logoutButton.contentMode = UIViewContentModeCenter;
//logoutButton.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
//logoutButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
AFAIK, UIControlContentVerticalAlignmentCenter
和contentHorizontalAlignment
用於文本對齊而不用於圖像...。
UIButton *logoutButton = [[UIButton alloc] initWithFrame:logoutButtonFrame];
將創建與“自定義”按鈕相對的基本按鈕(例如<iOS 6)。 以下是您應如何以編程方式設置按鈕的方式。
UIButton * logoutButton = [UIButton buttonWithType:UIButtonTypeCustom];
[logoutButton setBackgroundImage:[UIImage imageNamed:@"logout.png"] forControlEvents:UIControlEventTouchUpInside];
[logoutButton addTarget:self action:@selector(confirmLogout) forControlEvents:UIControlEventTouchUpInside];
[logoutButton setTitle:@"" forState:UIControlStateNormal];
logoutButton.frame = CGRectMake(logoutButtonX, 0, logoutButtonSize, logoutButtonSize);
[self.view addSubview: logoutButton];
現在要調整圖像
continueButton.imageView.contentMode = UIViewContentModeScaleAspectFit;
大功告成...
您的代碼無法正常工作,因為您的UIButton不是“自定義”按鈕。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.