bluetooth99 2013-07-29 03:21 采纳率: 0%
浏览 7883
已采纳

在UIButton标题上居中图片

有一个自定义UIButton,占了视图的四分之一,我想要在按钮标题上设置一张图片,居中设置。

代码:

CGRect imageFrame= CGRectMake(0, 0, 80, 80);
UIEdgeInsets imageInset = UIEdgeInsetsMake(0, 0, 30, 0);
UIEdgeInsets titleInset = UIEdgeInsetsMake(80, 0, 0, 0);

btnSideSettings = [UIButton buttonWithType:UIButtonTypeCustom];
btnSideSettings.frame = settingsFrame;
[btnSideSettings setImage:[UIImage imageNamed:@"btnSideSettings"] forState:UIControlStateNormal];
btnSideSettings.imageView.frame = imageFrame;
btnSideSettings.imageEdgeInsets = imageInset;
btnSideSettings.titleEdgeInsets = titleInset;
btnSideSettings.titleLabel.textAlignment = NSTextAlignmentCenter;
[btnSideSettings setTitle:@"Settings" forState:UIControlStateNormal];

结果:

CSDN移动问答

如果注释掉添加图片,标题就会居中。

请明白人指点一下,谢谢。

  • 写回答

1条回答 默认 最新

  • ReyZhang 移动开发领域新星创作者 2013-07-30 03:16
    关注

    可以换种方式来实现 。我这里的方式是用UIImageView 来倒圆角实现 .注意这里要导入一个库

    #import <QuartzCore/QuartzCore.h>
    
    UIImage *image=[UIImage imageNamed:@"btnSideSettings"];
    UIImageView *imgView=[[UIImageView alloc] initWithImage:image];
    
    /////给imgView进行圆形遮罩
    imgView.layer.masksToBound=YES;  ////required
    imgView.layer.cornerRadius=imgView.frame.size.width/2;  /////radiu is a half of imageview's width
    

    如果在图片上有点击动作可用手势来实现 。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog