如何将UIImage居中到UIButton?

4
我想知道如何将UIImage居中到UIButton上。我正在使用UIButton的setImage方法来放置UIImage。
3个回答

14
您可以使用contentMode属性:
button.imageView.contentMode = UIViewContentModeCenter;

3
即使如此,UIButton仍以右对齐方式显示图像。 - Frederic Yesid Peña Sánchez

5

在设置内容模式之前,请从UIButton中删除文本


2
有时候您可能会遇到错误的内容对齐。请确保两者都居中使用:

button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
button.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接