ios - 在 Button iOS 的中心对齐按钮

标签 ios objective-c ios7.1

我有一个按钮,其大小可能会根据需要而变化。但我需要设置在其上的图像始终位于其中心。

我现在使用的代码是:

[pAddButton setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter];
    [pAddButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentCenter];
    [pAddButton setContentMode:UIViewContentModeCenter];

但是,当我增加按钮的宽度时,图像会自动拉伸(stretch)。这就是我要避免的。

最佳答案

尝试将此代码图像框架添加到按钮

[image drawInRect:CGRectMake((self.frame.size.width/2) - (image.size.width/2), (self.frame.size.height / 2) - (image.size.height / 2), image.size.width, image.size.height)];

关于ios - 在 Button iOS 的中心对齐按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26255233/

相关文章:

ios - 有没有一种有效的方法可以快速地将 float 舍入到最近的百分之一位置?

ios - 没有结果时 Firebase 查询不调用 block

ios - 如何对数组中的日期进行分组?

ios - 更改字体后在 UITextField 上调用 sizeToFit 在 iOS 7.1 上无法正常工作

ios - iOS 7.1 iBeacon 监控更新对云台有何影响?

ios - UITableViewCell 动画错误

ios - 在 Storyboard中嵌入水平 ScrollView

iphone - 如何每15分钟执行一次后台任务?

ios - 如何使用Objective-C从纯文本远程文件中读取内容

ios单元格内容超出单元格框架