张洋给的自定义UIButton的title和image位置的方法:
1.继承自UIButton
2.重写这两个方法:
|
-(CGRect)titleRectForContentRect:(CGRect)contentRect { CGFloat titleY = contentRect.size.height *0.6; CGFloat titleW = contentRect.size.width; CGFloat titleH = contentRect.size.height - titleY; return CGRectMake(0, titleY, titleW, titleH); }
-(CGRect)imageRectForContentRect:(CGRect)contentRect { CGFloat imageW = CGRectGetWidth(contentRect); CGFloat imageH = contentRect.size.height * 0.6; return CGRectMake(0, 0, imageW, imageH); } |
本文介绍了一种自定义UIButton中标题和图片布局的方法。通过继承UIButton并重写两个关键方法,可以实现按钮上文本和图标的位置调整。这种方法适用于需要精确控制按钮元素显示的应用场景。
5063

被折叠的 条评论
为什么被折叠?



