我以编程方式创建了一个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设置不同的背景颜色后,我可以看到它仍然与底部对齐:
(蓝色=按钮,红色=图像视图)
为什么不起作用?
答案 0 :(得分:2)
根据您发布的图片,看起来按钮本身被剪裁在底部,而不是图像没有垂直居中。
您的代码:
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;
答案 1 :(得分:1)
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不是自定义按钮。