【问题标题】:UIButton contentVerticalAlignment doesn't center button imageUIButton contentVerticalAlignment 不居中按钮图像
【发布时间】:2017-06-03 15:33:58
【问题描述】:

我以编程方式创建了一个 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 设置不同的背景颜色后,我可以看到它仍然与底部对齐:

(蓝色 = 按钮,红色 = 图像视图)

为什么它不起作用?

【问题讨论】:

  • 检查我的答案,它会工作

标签: ios objective-c uibutton


【解决方案1】:

根据您发布的图片,看起来按钮本身在底部被剪裁,而不是图片没有垂直居中。

您的代码:

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;

【讨论】:

  • 你是对的,按钮在底部被剪掉了。谢谢!
【解决方案2】:

第 1 点

AFAIK, UIControlContentVerticalAlignmentCenter & contentHorizontalAlignment 用于文本对齐而不是图像....

第 2 点

UIButton *logoutButton = [[UIButton alloc] initWithFrame:logoutButtonFrame]; 将创建与自定义按钮相对的基本按钮(如

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 不是自定义按钮。

【讨论】:

  • 感谢自定义按钮提示,我不知道它会影响什么。但是,我发现图像没有对齐,因为按钮在底部被切断了。再次感谢!
  • 没有 UIButton 的接口声明选择器 setBackgroundImage:forControlEvents: 最接近的是 setBackgroundImage:forState: 你可能的意思是 [logoutButton setBackgroundImage:[UIImage imageNamed:@"logout.png"] forState:UIControlStateNormal] ;
猜你喜欢
  • 2021-12-14
  • 2022-01-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-16
  • 1970-01-01
相关资源
最近更新 更多