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 Stata 面板数据模型选择
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用