iphone - UIEdgeInsets 定位文本/图像问题

标签 iphone image uibutton title

我尝试制作一个 UIButton ,图像位于文本上方,因此我使用以下代码:

UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(x, y, 140, 140);
[btn setTitle:self.objMateria.titoloMateria forState:UIControlStateNormal];
[btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[btn setBackgroundColor:[UIColor clearColor]];
[btn setImage:[UIImage imageNamed:@"pala.png"] forState:UIControlStateNormal];
btn.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 20, 0);
btn.titleEdgeInsets = UIEdgeInsetsMake(120, 0, 0, 0);

问题是,使用这段代码我看不到文本,它似乎被图像覆盖了,错误在哪里?

这里是图片:
enter image description here

最佳答案

试试这个

btn.imageEdgeInsets = UIEdgeInsetsMake(-20, 0, 0, 0);
btn.titleEdgeInsets = UIEdgeInsetsMake(120, -120, 0, 0);

调整-120的值,即left以水平适合您的标题

关于iphone - UIEdgeInsets 定位文本/图像问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13915898/

相关文章:

iphone - NSTimer 不会停止

iphone - 当应用程序转到后台时如何创建 "return to app"状态栏?

python - Python-如何根据其值裁剪图像?

iphone - 应用程序启动时进行应用内购买检查

android - Android中的相机,如何获得最佳尺寸,预览尺寸,图片尺寸, View 尺寸,图像失真

java Android - 以编程方式处理图像缩放/裁剪

ios - UIButton 中的 Swift 3D Touch Force Value

ios - 未调用 UITableView 的触摸应取消功能

ios - 如何使按钮变灰以让用户知道它当前在 iOS 中被禁用?

html - 在 iOS 上将 HTML 转换为 PDF 时出现问题