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 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作