我有一个按钮,其大小可能会根据需要而变化。但我需要设置在其上的图像始终位于其中心。
我现在使用的代码是:
[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/